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. 重学DSP:对于卷积的理解

    最近,我发现自己对于一个事情,如果不给自己一个说服自己的理由,就会出现不能理解,不能记住,以至于不会使用或者“盲目”应用的情况. 但是,我学的这个学科就是应当建立在对信号作用过程的理解上面的. 下面, ...

  2. hadoop,帮我解了部分惑的文章

    http://blog.csdn.net/qianshangding0708/article/details/47423613

  3. FFMPEG more samples than frame size (avcodec_encode_audio2) 的解决方案

    在实际的项目中,从音频设备采集到的音频的类型和编码器类型(aac ,amr)通常是不一致的. 那么我们首先需要做重采样的过程.利用swr_convert 重新采样. 这时候我们可能会遇到另外一个问题. ...

  4. codeforces C. New Year Ratings Change 解题报告

    题目链接:http://codeforces.com/problemset/problem/379/C 题目意思:有n个users,每个user都有自己想升的rating.要解决的问题是给予每个人不同 ...

  5. 树堆(Treap)

    平衡树 简介: 平衡二叉树(Balanced Binary Tree)具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.平衡二叉树的常用实现方 ...

  6. Constructing Roads In JGShining's Kingdom

    点击打开题目链接 本题目是考察  最长递增子序列的  有n^2     n(logn)  n^2  会超时的 下面两个方法的代码  思路  可以百度LIS  LCS dp里面存子序列 n(logn) ...

  7. swprintf has been changed to conform with the ISO C standard, adding an extra character count parameter.

    'swprintf': swprintf has been changed to conform with the ISO C standard, adding an extra character ...

  8. ARM、DSP、FPGA的技术特点和区别

    在嵌入式开发领域,ARM是一款非常受欢迎的微处理器,其市场覆盖率极高,DSP和FPGA则是作为嵌入式开发的协处理器,协助微处理器更好的实现产品功能. 那三者的技术特点以及区别是什么呢?下文就此问题略做 ...

  9. HttpClient入门教程

    HttpClient使用详解与实战一:https://www.jianshu.com/p/375be5929bed

  10. MR 图像分割 相关论文摘要整理

    <多分辨率水平集算法的乳腺MR图像分割> 针对乳腺 MR 图像信息量大.灰度不均匀.边界模糊.难分割的特点, 提出一种多分辨率水平集乳腺 MR图像分割算法. 算法的核心是首先利用小波多尺度 ...