BZOJ 2337: [HNOI2011]XOR和路径( 高斯消元 )
2015-12-02 16:37
375 查看
![](http://images2015.cnblogs.com/blog/723896/201512/723896-20151202163221814-595033854.png)
一位一位考虑异或结果, f(x)表示x->n异或值为1的概率, 列出式子然后高斯消元就行了
------------------------------------------------------------------
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath> using namespace std; typedef long double ld;#define b(i) (1 << (i)) const int maxn = 109; ld mat[maxn][maxn];int N, deg[maxn]; struct edge { int to, w; edge* next;} E[20009], *pt = E, *head[maxn]; void AddEdge(int u, int v, int w) { deg[pt->to = v]++; pt->w = w; pt->next = head[u]; head[u] = pt++;} void Init() { memset(deg, 0, sizeof deg); int m; scanf("%d%d", &N, &m); N--; for(int i = 0; i < m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); u--; v--; AddEdge(u, v, w); if(u != v) AddEdge(v, u, w); }} void Work() { for(int i = 0; i < N; i++) { int r = i; for(int j = i; ++j < N; ) if(fabs(mat[j][i]) > fabs(mat[r][i])) r = j; if(r != i) { for(int j = 0; j <= N; j++) swap(mat[i][j], mat[r][j]); } for(int j = i; ++j < N; ) { ld t = mat[j][i] / mat[i][i]; for(int k = i; k <= N; k++) mat[j][k] -= t * mat[i][k]; } } for(int i = N; i--; ) { for(int j = i; ++j < N; ) mat[i]
-= mat[i][j] * mat[j]
; mat[i]
/= mat[i][i]; }} int main() { Init(); ld ans = 0; for(int i = 0; i < 30; i++) { memset(mat, 0, sizeof mat); for(int j = 0; j < N; j++) { for(edge* e = head[j]; e; e = e->next) if(e->to != N) { if(e->w & b(i)) mat[j][e->to]--, mat[j]
--; else mat[j][e->to]++; } else if(e->w & b(i)) mat[j]
--; mat[j][j] -= deg[j]; } Work(); ans += mat[0]
* b(i); } printf("%.3lf\n", (double) ans); return 0;}------------------------------------------------------------------
2337: [HNOI2011]XOR和路径
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 699 Solved: 390
[Submit][Status][Discuss]
Description
![](http://www.lydsy.com/JudgeOnline/upload/201105/2.jpg)
Input
Output
Sample Input
Sample Output
HINT
Source
Day2相关文章推荐
- Servlet的urlpattern匹配规则
- 有用的导航栏下拉代码和调用时间日期函数
- ASP.NET IIS设置 Session时间
- java.lang.IllegalStateException: Parameter count exceeded allowed maximum错误解决
- 自己动手做一个Mini Linux
- 探秘 C stdlib.h
- linux伙伴算法实现过程分析
- hdu 1269 迷宫城堡 强连通分量
- android 打开软键盘
- 使用eagle导出PCB制造商可用的gerber文件
- 使类和成员的可访问性最小化
- 网络模拟工具
- Java 并发
- 【Leetcode】Product of Array Except Self
- Gerrit no Verified 在最新的2.7版本之后添加Verified
- 1052. Linked List Sorting
- 一个文件上传模块
- footer根据根据内容是否满屏选择是否居于底部
- 使用Volley上传文件
- nodejs创建一个应用