http://acm.hdu.edu.cn/showproblem.php?pid=3359

题目的意思是,由矩阵A生成矩阵B的方法是:

以a[i][j]为中心的,哈曼顿距离不大于dis的数字的总和 / 个数,就是矩阵B的b[i][j]

现在给出B,要求A

那么我们设A矩阵为a[1][1], a[1][2], a[1][3].....

那么对于每一个b[i][j]我们有b[i][j] = (a[1][1] + a[1][2] + ... + ) / cnt

所以这样可以建议一条方程,然后guass求解。

注意题目的输出格式,printf("%8.2lf")后,不需要加空格。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 2e2 + ;
const double eps = 1e-;
class GaussMatrix {
public:
double a[maxn][maxn];
int equ, val; //方程个数(行),和变量个数(列),其中第val个是b值,不能取
void swapRow(int rowOne, int rowTwo) {
for (int i = ; i <= val; ++i) {
swap(a[rowOne][i], a[rowTwo][i]);
}
}
void swapCol(int colOne, int colTwo) {
for (int i = ; i <= equ; ++i) {
swap(a[i][colOne], a[i][colTwo]);
}
}
bool same(double x, double y) {
return fabs(x - y) < eps;
}
int guass() {
int k, col;
for (k = , col = ; k <= equ && col < val; ++k, ++col) { //不能取到第val个
int maxRow = k; //选出列最大值,误差最小
for (int i = k + ; i <= equ; ++i) {
if (fabs(a[i][col]) > fabs(a[maxRow][col])) {
maxRow = i;
}
}
if (same(a[maxRow][col], )) {
--k;
continue;
}
if (maxRow != k) swapRow(k, maxRow);
for (int i = col + ; i <= val; ++i) { //约去系数
a[k][i] /= a[k][col];
}
a[k][col] = 1.0; //第一个就要变成1了,然后下面和上面的变成0
for (int i = ; i <= equ; ++i) {
if (i == k) continue; //当前这行,不操作
for (int j = col + ; j <= val; ++j) {
a[i][j] -= a[i][col] * a[k][j];
}
a[i][col] = 0.0;
}
// debug();
}
for (k; k <= equ; ++k) {
if (!same(a[k][val], )) return -; //方程无解
}
return val - k; //自由变量个数
}
void debug() {
for (int i = ; i <= equ; ++i) {
for (int j = ; j <= val; ++j) {
printf("%6.2lf ", a[i][j]);
}
printf("\n");
}
printf("*******************************************\n\n");
}
}arr;
int dis;
double mp[maxn][maxn];
int vis[maxn][maxn], DFN;
int n, m;
int tonext[][] = {{, }, {, }, {, -}, {-, }};
struct bfsNode {
int cnt, x, y;
bfsNode(int _cnt, int _x, int _y) {
cnt = _cnt, x = _x, y = _y;
}
};
queue<struct bfsNode>que;
int toHash(int x, int y) {
return x * max(n, m) + y;
}
void init(int row, int col, int which) {
++DFN;
while (!que.empty()) que.pop();
arr.a[which][toHash(row, col)] = 1.0;
que.push(bfsNode(, row, col));
vis[row][col] = DFN;
int has = ;
while (!que.empty()) {
struct bfsNode t = que.front();
que.pop();
if (t.cnt + > dis) break;
for (int i = ; i < ; ++i) {
int tx = t.x + tonext[i][], ty = t.y + tonext[i][];
if (tx >= && tx <= n && ty >= && ty <= m && vis[tx][ty] != DFN) {
vis[tx][ty] = DFN;
arr.a[which][toHash(tx, ty)] = 1.0;
que.push(bfsNode(t.cnt + , tx, ty));
has++;
}
}
}
arr.a[which][toHash(n, m) + ] = mp[row][col] * has;
}
void work() {
n = arr.equ, m = arr.val;
for (int i = ; i <= n; ++i) {
for (int j = ; j <= m; ++j) {
scanf("%lf", &mp[i][j]);
}
}
int which = ;
for (int i = ; i <= n; ++i) {
for (int j = ; j <= m; ++j) {
init(i, j, ++which);
}
}
arr.equ = which;
arr.val = toHash(n, m) + ;
// arr.debug();
arr.guass();
// arr.debug();
int to = ;
for (int i = ; i <= n; ++i) {
for (int j = ; j <= m; ++j) {
printf("%8.2lf", arr.a[to++][toHash(n, m) + ]);
}
printf("\n");
}
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
while (scanf("%d%d%d", &arr.val, &arr.equ, &dis) != EOF && arr.val + arr.equ + dis) {
if (!flag) printf("\n");
flag = false;
work();
memset(&arr, , sizeof arr);
}
return ;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e2 + ;
const double eps = 1e-;
class GaussMatrix {
public:
double a[maxn][maxn], x[maxn];
int equ, val; //方程个数(行),和变量个数(列),其中第val个是b值,不能取
void swapRow(int rowOne, int rowTwo) {
for (int i = ; i <= val; ++i) {
swap(a[rowOne][i], a[rowTwo][i]);
}
}
void swapCol(int colOne, int colTwo) {
for (int i = ; i <= equ; ++i) {
swap(a[i][colOne], a[i][colTwo]);
}
}
bool same(double x, double y) {
return fabs(x - y) < eps;
}
int guass() {
int k, col;
for (k = , col = ; k <= equ && col < val; ++k, ++col) { //不能取到第val个
int maxRow = k; //选出列最大值,误差最小
for (int i = k + ; i <= equ; ++i) {
if (fabs(a[i][col]) > fabs(a[maxRow][col])) {
maxRow = i;
}
}
if (same(a[maxRow][col], )) {
--k;
continue;
}
if (maxRow != k) swapRow(k, maxRow);
for (int i = col + ; i <= val; ++i) { //约去系数
a[k][i] /= a[k][col];
}
a[k][col] = 1.0; //第一个就要变成1了,然后下面和上面的变成0
for (int i = ; i <= equ; ++i) {
if (i == k) continue; //当前这行,不操作
for (int j = col + ; j <= val; ++j) {
a[i][j] -= a[i][col] * a[k][j];
}
a[i][col] = 0.0;
}
debug();
}
for (k; k <= equ; ++k) {
if (!same(a[k][val], )) return -; //方程无解
}
return val - k; //自由变量个数
}
void debug() {
for (int i = ; i <= equ; ++i) {
for (int j = ; j <= val; ++j) {
printf("%6.2lf ", a[i][j]);
}
printf("\n");
}
printf("*******************************************\n\n");
}
}arr;
void work() {
// arr.equ = 3, arr.val = 5;
// arr.a[1][1] = 1, arr.a[1][2] = 2, arr.a[1][3] = -1, arr.a[1][4] = 1, arr.a[1][5] = 2;
// arr.a[2][1] = 2, arr.a[2][2] = -1, arr.a[2][3] = 1, arr.a[2][4] = -3, arr.a[2][5] = -1;
// arr.a[3][1] = 4, arr.a[3][2] = 3, arr.a[3][3] = -1, arr.a[3][4] = -1, arr.a[3][5] = 3;
// int res = arr.guass();
// cout << res << endl; // arr.equ = 4, arr.val = 3 + 1;
// arr.a[1][1] = 2, arr.a[1][2] = 3, arr.a[1][3] = 1, arr.a[1][4] = 4;
// arr.a[2][1] = 1, arr.a[2][2] = -2, arr.a[2][3] = 4, arr.a[2][4] = -5;
// arr.a[3][1] = 3, arr.a[3][2] = 8, arr.a[3][3] = -2, arr.a[3][4] = 13;
// arr.a[4][1] = 4, arr.a[4][2] = -1, arr.a[4][3] = 9, arr.a[4][4] = -6;
// cout << arr.guass() << endl; // arr.equ = 3, arr.val = 3 + 1;
// arr.a[1][1] = 2, arr.a[1][2] = 3, arr.a[1][3] = 1, arr.a[1][4] = 16;
// arr.a[2][1] = 1, arr.a[2][2] = 5, arr.a[2][3] = 2, arr.a[2][4] = 23;
// arr.a[3][1] = 3, arr.a[3][2] = 4, arr.a[3][3] = 5, arr.a[3][4] = 33;
// cout << arr.guass() << endl; // arr.equ = 3, arr.val = 4;
// arr.a[1][1] = 2, arr.a[1][2] = 3, arr.a[1][3] = -1, arr.a[1][4] = 2;
// arr.a[2][1] = 3, arr.a[2][2] = -2, arr.a[2][3] = 1, arr.a[2][4] = 2;
// arr.a[3][1] = 1, arr.a[3][2] = -5, arr.a[3][3] = 2, arr.a[3][4] = 1;
// cout << arr.guass() << endl; arr.equ = , arr.val = ;
arr.a[][] = , arr.a[][] = , arr.a[][] = , arr.a[][] = ;
arr.a[][] = , arr.a[][] = , arr.a[][] = , arr.a[][] = ;
arr.a[][] = , arr.a[][] = , arr.a[][] = , arr.a[][] = ;
cout << arr.guass() << endl;
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

高斯消元模板 && 题目

HDU 3359 高斯消元模板题,的更多相关文章

  1. 高斯消元模板!!!bzoj1013

    /* 高斯消元模板题 n维球体确定圆心必须要用到n+1个点 设圆心坐标(x1,x2,x3,x4...xn),半径为C 设第i个点坐标为(ai1,ai2,ai3,,,ain)那么对应的方程为 (x1-a ...

  2. HDU 2827 高斯消元

    模板的高斯消元.... /** @Date : 2017-09-26 18:05:03 * @FileName: HDU 2827 高斯消元.cpp * @Platform: Windows * @A ...

  3. hdu 3915 高斯消元

    http://acm.hdu.edu.cn/showproblem.php?pid=3915 这道题目是和博弈论挂钩的高斯消元.本题涉及的博弈是nim博弈,结论是:当先手处于奇异局势时(几堆石子数相互 ...

  4. [置顶] hdu 4418 高斯消元解方程求期望

    题意:  一个人在一条线段来回走(遇到线段端点就转变方向),现在他从起点出发,并有一个初始方向, 每次都可以走1, 2, 3 ..... m步,都有对应着一个概率.问你他走到终点的概率 思路: 方向问 ...

  5. 【BZOJ 1013】【JSOI2008】球形空间产生器sphere 高斯消元基础题

    最基础的高斯消元了,然而我把j打成i连WA连跪,考场上再犯这种错误就真的得滚粗了. #include<cmath> #include<cstdio> #include<c ...

  6. 【Luogu】P3389高斯消元模板(矩阵高斯消元)

    题目链接 高斯消元其实是个大模拟qwq 所以就着代码食用 首先我们读入 ;i<=n;++i) ;j<=n+;++j) scanf("%lf",&s[i][j]) ...

  7. HDU 4418 高斯消元解决概率期望

    题目大意: 一个人在n长的路径上走到底再往回,走i步停下来的概率为Pi , 求从起点开始到自己所希望的终点所走步数的数学期望 因为每个位置都跟后m个位置的数学期望有关 E[i] = sigma((E[ ...

  8. POJ 1830 【高斯消元第一题】

    首先...使用abs()等数学函数的时候,浮点数用#include<cmath>,其它用#include<cstdlib>. 概念: [矩阵的秩] 在线性代数中,一个矩阵A的列 ...

  9. 【转】高斯消元模板 by kuangbin

    写的很好,注释很详细,很全面. 原blog地址:http://www.cnblogs.com/kuangbin/archive/2012/09/01/2667044.html #include< ...

随机推荐

  1. Eclipse jar打包详解

    通过Eclipse下的演示工程,介绍如何打包这样的项目:要导出的类里边用到了别的jar包. 方法/步骤     1. Eclipse下的演示工程结构如下图所示,其中Task.java是当前工程运行的M ...

  2. 花了5天时间,终于解决了一个bug,心情非常愉快,憋了这么久,不吐不快

    http://www.cnweblog.com/fly2700/archive/2011/12/06/318916.html (转载) 花了5天时间,终于解决了一个bug,心情非常愉快,憋了这么久,不 ...

  3. mongodb给我们提供了fsync+lock机制把数据暴力的刷到硬盘上

    能不能把数据暴力的刷到硬盘上,当然是可以的,mongodb给我们提供了fsync+lock机制就能满足我们提的需求. fsync+lock首先会把缓冲区数据暴力刷入硬盘,然后给数据库一个写入锁,其他实 ...

  4. 编译Thrift

    按照 https://syslint.com/blog/tutorial/how-to-install-apache-thrift-on-ubuntu-14-04/ 进行, 编译时出现错误 make[ ...

  5. 【C】论‘\r’和'\n'的纯粹性

  6. HihoCoder 1590 : 紧张的会议室(区间最大+离散化)

    时间限制:20000ms 单点时限:2000ms 内存限制:256MB 描述 小Hi的公司最近员工增长迅速,同时大大小小的会议也越来越多:导致公司内的M间会议室非常紧张. 现在小Hi知道公司目前有N个 ...

  7. Lucas定理和扩展Lucas定理

    1.Lucas定理 首先给出式子:\(C_n^m\%p = C_{\lfloor\frac{n}{p}\rfloor}^{\lfloor\frac{m}{p}\rfloor} * C_{n\%p}^{ ...

  8. es6 import 报错

    现在绝大多数的浏览器都不支持ES6,所以使用es6时需要使用bebal把es6转化为es5, 项目目录: demo1:单个js文件的转化 src文件下的 test1.js const aa=" ...

  9. 《Kubernetes权威指南第2版》学习(二)一个简单的例子

    1: 安装VirtualBox, 并下载CentOS-7-x86_64-DVD-1708.iso, 安装centOS7,具体过程可以百度. 2:开启centOS的SSH, 步骤如下: (1) yum ...

  10. bzoj1095

    动态点分治 先建出点分树,每个点上维护两个堆,s1,s2,分别表示子树中到点分树中父亲的所有长度,每个儿子s1的最大值,那么对于每个点答案就是s2的最大+次大,再维护一个s3保存这个. 首先我们要搞一 ...