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. codeforces C. Team 解题报告

    题目链接:http://codeforces.com/problemset/problem/401/C 题目意思:给出0和1的数目(分别为n和m个),问是否能构造一条同时满足连续两个0不能再一起和连续 ...

  2. hdu 1753 大明A+B(大数)

    题意:小数大数加法 思路:大数模板 #include<iostream> #include<stdio.h> #include<string.h> using na ...

  3. 使用grunt中遇到的问题

    1.使用jshint进行代码检查时,grunt命令后报错: 因为出现了乱码,我猜测是因为编码原因导致的.遂在webstorm的setting中修改了编码为utf-8,问题解决.

  4. CodeForces526F:Pudding Monsters (分治)

    In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...

  5. Java笔记(九)

    网络编程: UDP传输: (UdpSend发送端)通过UDP传输方式,将一段文字数据发送出去: (1)建立udpsocket服务 (2)提供数据,并将数据封装到数据包中 (3)通过socket服务的发 ...

  6. vue-router 获得上一级路由以及返回上一级路由的方法

    if (this.$store.state.previousRouter.name) { this.$router.push({name: this.$store.state.previousRout ...

  7. 如何更快更好的写出cnblog博客?windows live writer推荐

    之前总是会羡慕网上那些技术牛人的博客都写的那么给力,后来一搜发现还是有工具可用的. 这里就推荐一款写博客的"神器",Windows Live Writer (Get It Now! ...

  8. 禁用ubuntu 客人会话

    sudo vi /usr/share/lightdm/lightdm.conf.d/50-guest-wrapper.conf 添加: allow-guest=false 重启.

  9. ceph之ceph-client安装

    1.安装ceph-client ceph-deploy install ceph-client 2.创建块设备 [root@mon1 ~]# rbd create test1 --image-form ...

  10. PICO 中关于时基ps3000aGetTimebase函数介绍