BZOJ1013 + BZOJ1923 + POJ1830 (高斯消元)
三个题放在一起写了 主要是搞搞模板 在这里简述一下怎么写高斯消元
就和代数里学的加减消元学的一样 把矩阵化为上三角形形式 然后进行回代
同时枚举当前要消元的未知数和当前化简到哪一行了
然后从这一行往后 找这一列的一个不为0的系数
如果这一列以后的每一行都是0了 那么就说明当前这个未知数可以作为一个自由元 就是有无数解的意思
然后继续枚举下一个未知数
如果找到一个不为0的 和当前这一行的所有元素swap一下 然后除了这一行外 把其他所有行在这一列的系数消为0
最后答案存在每一行的第n + 1个位置
如果化简完了 如果存在后面的某一行 他的n + 1的值不等于0 那么就是无解
bzoj1013
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string.h>
using namespace std;
double eps = 1e-; int n;
double a[][];
double zb[][]; void gauss()
{
int now = , to;
for(int i = ; i <= n; i++)
{
for(to = now; to <= n; to++) if(fabs(a[to][i]) > eps) break;
if(to > n) continue; if(to != now)
for(int j = ; j <= n + ; j++) swap(a[to][j], a[now][j]); double tmp = a[now][i];
for(int j = ; j <= n + ; j++) a[now][j] /= tmp;
for(int j = ; j <= n; j++)
if(j != now)
{
tmp = a[j][i];
for(int k = ; k <= n + ; k++) a[j][k] -= tmp * a[now][k];
}
now++;
}
} int main()
{
cin>>n;
for(int i = ; i <= n + ; i++)
{
for(int j = ; j <= n; j++) scanf("%lf", &zb[i][j]);
if(i > )
for(int j = ; j <= n; j++) a[i - ][j] = 2.0 * (zb[i][j] - zb[][j]), a[i - ][n + ] += zb[i][j] * zb[i][j] - zb[][j] * zb[][j];
}
gauss();
for(int i = ; i <= n - ; i++) printf("%.3lf ", a[i][n + ]);
printf("%.3lf\n", a[n][n + ]);
return ;
}
bzoj1923 高斯消元的时间复杂度是n三方的 然后这个题数据是1000 10s居然水过去了 听说有用bitset优化的方法 (以后再学吧
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std; int n, m, x, ans;
int a[][]; void gauss1()
{
int now = , to;
for(int i = ; i <= n; i++)
{
for(to = now; to <= m; to++) if(a[to][i]) break;
if(to > m)
{
ans = -;
return;
}
ans = max(ans, to); if(to != now)
for(int j = ; j <= n + ; j++) swap(a[to][j], a[now][j]); for(int j = ; j <= m; j++)
if(j != now && a[j][i])
for(int k = ; k <= n + ; k++) a[j][k] ^= a[now][k];
now++;
}
} int main()
{
cin>>n>>m;
for(int i = ; i <= m; i++)
{
char s[];
scanf("%s %d", s, &x);
int len = strlen(s);
for(int j = ; j < len; j++) a[i][j + ] = s[j] - '';
a[i][len + ] = x;
} gauss1();
if(ans == -) puts("Cannot Determine");
else
{
printf("%d\n", ans);
for(int i = ; i <= n; i++)
{
if(a[i][n + ]) puts("?y7M#");
else puts("Earth");
}
}
return ;
}
POJ1830 入门题
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std; int ans;
int q[];
int w[];
int a[][]; void gauss(int n, int m)
{
ans = ;
int now = , to;
for(int i = ; i <= n; i++)
{
for(to = now; to <= m; to++) if(a[to][i]) break;
if(to > m)
{
ans++;
continue;
} if(to != now)
for(int j = ; j <= n + ; j++) swap(a[to][j], a[now][j]); for(int j = ; j <= m; j++)
if(j != now && a[j][i])
for(int k = ; k <= n + ; k++) a[j][k] ^= a[now][k];
now++;
}
for(int i = now; i <= m; i++)
if(a[i][n + ])
{
ans = -;
return;
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(a, , sizeof(a));
int n; scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%d", &q[i]);
for(int i = ; i <= n; i++) scanf("%d", &w[i]), w[i] ^= q[i], a[i][n + ] = w[i];
for(int i = ; i <= n; i++) a[i][i] = ; int u, v;
while(~scanf("%d%d", &u, &v) && u + v) a[v][u] = ;
gauss(n, n);
if(ans == -) puts("Oh,it's impossible~!!");
else printf("%d\n", << ans);
}
return ;
}
BZOJ1013 + BZOJ1923 + POJ1830 (高斯消元)的更多相关文章
- poj1830(高斯消元解mod2方程组)
题目链接:http://poj.org/problem?id=1830 题意:中文题诶- 思路:高斯消元解 mod2 方程组 有 n 个变元,根据给出的条件列 n 个方程组,初始状态和终止状态不同的位 ...
- *POJ1830 高斯消元
开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8010 Accepted: 3161 Description ...
- 【BZOJ1013】球形空间产生器(高斯消元)
[BZOJ1013]球形空间产生器(高斯消元) 题面 Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球 面上n+1个点的坐标, ...
- 【BZOJ1013】【JSOI2008】球形空间产生器 高斯消元
题目描述 有一个\(n\)维空间中的球,告诉你球面上\(n+1\)个点的坐标,求球心的坐标. \(n\leq 10\) 题解 设\(a_{i,j}\)为第\(i\)个点的第\(j\)维坐标,\(i=0 ...
- 【题解】 bzoj1923: [Sdoi2010]外星千足虫 (线性基/高斯消元)
bzoj1923,戳我戳我 Solution: 这个高斯消元/线性基很好看出来,主要是判断在第K 次统计结束后就可以确定唯一解的地方和\(bitset\)的骚操作 (我用的线性基)判断位置,我们可以每 ...
- BZOJ1013 JSOI2008 球形空间产生器sphere 【高斯消元】
BZOJ1013 JSOI2008 球形空间产生器sphere Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球面上n+1个点 ...
- 【BZOJ1923】[Sdoi2010]外星千足虫 高斯消元
[BZOJ1923][Sdoi2010]外星千足虫 Description Input 第一行是两个正整数 N, M. 接下来 M行,按顺序给出 Charles 这M次使用“点足机”的统计结果.每行 ...
- BZOJ1013球形空间产生器sphere 高斯消元
@[高斯消元] Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球 面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球 ...
- LG2447/BZOJ1923 「SDOI2010」外星千足虫 高斯消元
问题描述 LG2447 BZOJ1923 题解 显然是一个高斯消元,但是求的东西比较奇怪 发现这个方程组只关心奇偶性,于是可以用一个\(\mathrm{bitset}\)进行优化,用xor来进行消元操 ...
随机推荐
- 在.Net MVC结构API接口中推断http头信息实现公共的权限验证过滤器演示样例
//control action public class TestController : ApiController { [MyAuthFilter] public string test(s ...
- rk3188调试记录
1.编译 # . build/envsetup.sh # lunch 7 7. PI3910-user 1.电池部分 init.rc启动healthd-charger服务.对电池进行检測 se ...
- the largest value you actually can transmit between the client and server is determined by the amount of available memory and the size of the communications buffers.
the largest value you actually can transmit between the client and server is determined by the amoun ...
- [Android6.0][RK3399] 双屏异显代码实现流程分析(二)【转】
本文转载自:http://blog.csdn.net/dearsq/article/details/55050125 Patch Code dtsi rk3399-androiddtsi rk3399 ...
- Interfaces (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173156.aspx An interface contains definitions for a group ...
- POJ 2977 Box walking 长方体表面两点距离
POJ2977 小学生的考试题,暴力得出O(1)的解法 #include<iostream> #include<cstdio> #include<cstdlib> ...
- openstack network:dhcp binding fail
- EasyUI Form表单提交
转自:https://www.cnblogs.com/net5x/articles/4576926.html Form(表单) 使用$.fn.form.defaults重写默认值对象 form提供了各 ...
- bzoj 1801: [Ahoi2009]chess 中国象棋【dp】
注意到一行只能放012个炮,我们只需要知道列的状态,不用状压行 所以设f[i][j][k]表示前i行有j列有1个炮,有k列有2个炮的方案数 然后分情况讨论转移就行了 #include<cstdi ...
- bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士【bfs】
bfs预处理出每个点s和t的距离d1和d2(无法到达标为inf),然后在若干灌木丛格子(x,y)里取min(d1[x][y]+d2[x][y]) /* 0:贝茜可以通过的空地 1:由于各种原因而不可通 ...