依据题意可构造出方程组。方程组的每一个方程格式均为:C1*x1 + C2*x2 + ...... + C9*x9 = sum + 4*ki;

高斯消元构造上三角矩阵,以最后一个一行为例:

C*x9 = sum + 4*k。exgcd求出符合范围的x9,其它方程在代入已知的变量后格式亦如此。

第一发Gauss。蛮激动的。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <ctime> #pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define INF 0x3f3f3f3f
#define Mod 6000007 using namespace std; const int MAXN = 20; int up[] = {0,4,3,4,3,5,3,4,3,4}; int site[10][5] = {
{0},
{1,2,4,5},
{1,2,3},
{2,3,5,6},
{1,4,7},
{2,4,5,6,8},
{3,6,9},
{4,5,7,8},
{7,8,9},
{5,6,8,9}
}; int Map[10]; LL coe[MAXN][MAXN];
LL sol[MAXN]; void Output()
{
int i,j;
for(i = 1;i <= 9; ++i)
{
for(j = 1;j <= 10; ++j)
{
printf("%lld ",coe[i][j]);
if(j == 9)
printf("= ");
}
printf("\n");
}
puts("");
} LL Abs(LL x)
{
if(x < 0)
return -x;
return x;
} LL gcd(LL x,LL y)
{
if(y == 0)
return x;
return gcd(y,x%y);
} void exgcd(LL a,LL b,LL &x,LL &y)
{
if(b == 0)
x = 1,y = 0;
else
{
LL x1,y1;
exgcd(b,a%b,x1,y1);
x = y1;
y = x1-a/b*y1;
}
} //n为行数,m为列数(包括最后一项)
//return -1无整数解 return 0存在整数解。
int Gauss(int n,int m)
{
int i,j,k; LL T,A,B; //Output(); for(i = 1;i < n; ++i)
{
for(j = i+1;j <= n; ++j)
{
if(coe[j][i] == 0)
continue; if(coe[i][i] == 0)
{
for(k = i;k <= m; ++k)
T = coe[i][k],coe[i][k] = coe[j][k],coe[j][k] = T;
continue;
} T = gcd(coe[i][i],coe[j][i]);
A = coe[j][i]/T,B = coe[i][i]/T; for(k = i;k <= m; ++k)
coe[j][k] = coe[i][k]*A - coe[j][k]*B;
}
//Output();
} LL sum = 0; for(i = n;i >= 1; --i)
{
sum = coe[i][m];
for(j = m-1;j > i; --j)
sum -= coe[i][j]*sol[j]; LL A = coe[i][i],B = 4,C = sum;
LL x,y; exgcd(A,B,x,y);
//cout<<"A = "<<A<<" B = "<<B<<" C = "<<C<<" x = "<<x<<" y = "<<y<<endl;
x *= C/gcd(A,B);
//cout<<"x = "<<x<<endl;
y = B/gcd(A,B);
x = (x-x/y*y + Abs(y))%Abs(y);
sol[i] = x; //cout<<"i = "<<i<<" x = "<<x<<endl; // if(sum%coe[i][i] != 0)
// return -1;//此时无整数解
// sol[i] = sum/coe[i][i];
} return 0;
} int main()
{
int i,j; for(i = 1;i <= 9; ++i)
scanf("%d",&Map[i]); memset(coe,0,sizeof(coe)); for(i = 1;i <= 9; ++i)
{
for(j = 0;j < up[i]; ++j)
{
coe[site[i][j]][i] = 1;
}
} for(i = 1;i <= 9; ++i)
coe[i][10] = (4-Map[i])%4; if(-1 == Gauss(9,10))
while(0)
; bool mark = true; for(i = 1;i <= 9;++i)
{
for(j = 0;j < sol[i]; ++j)
{
if(mark == false)
printf(" ");
else
mark = false;
printf("%d",i);
}
} return 0;
}

POJ 1166 The Clocks 高斯消元 + exgcd(纯属瞎搞)的更多相关文章

  1. POJ 1830 开关问题 高斯消元,自由变量个数

    http://poj.org/problem?id=1830 如果开关s1操作一次,则会有s1(记住自己也会变).和s1连接的开关都会做一次操作. 那么设矩阵a[i][j]表示按下了开关j,开关i会被 ...

  2. A - The Water Bowls POJ - 3185 (bfs||高斯消元)

    题目链接:https://vjudge.net/contest/276374#problem/A 题目大意:给你20个杯子,每一次操作,假设当前是对第i个位置进行操作,那么第i个位置,第i+1个位置, ...

  3. poj The Clocks 高斯消元

    由于数据量不大,所以这题有很多解法. 我用的是高斯消元化为逆矩阵解决的…… 代码如下: #include<stdio.h> #include<iostream> using n ...

  4. POJ 2065 SETI(高斯消元)

    题目链接:http://poj.org/problem?id=2065 题意:给出一个字符串S[1,n],字母a-z代表1到26,*代表0.我们用数组C[i]表示S[i]经过该变换得到的数字.给出一个 ...

  5. POJ 1830 开关问题 (高斯消元)

    题目链接 题意:中文题,和上篇博客POJ 1222是一类题. 题解:如果有解,解的个数便是2^(自由变元个数),因为每个变元都有两种选择. 代码: #include <iostream> ...

  6. Poj 2947 widget factory (高斯消元解同模方程)

    题目连接: http://poj.org/problem?id=2947 题目大意: 有n种类型的零件,m个工人,每个零件的加工时间是[3,9],每个工人在一个特定的时间段内可以生产k个零件(可以相同 ...

  7. POJ 1753 Flip game ( 高斯消元枚举自由变量)

    题目链接 题意:给定一个4*4的矩阵,有两种颜色,每次反转一个颜色会反转他自身以及上下左右的颜色,问把他们全变成一种颜色的最少步数. 题解:4*4的矩阵打表可知一共有四个自由变元,枚举变元求最小解即可 ...

  8. POJ 3532 Resistance(高斯消元+基尔霍夫定理)

    [题目链接] http://poj.org/problem?id=3532 [题目大意] 给出n个点,一些点之间有电阻相连,求1~n的等效电阻 [题解] 有基尔霍夫定理:任何一个点(除起点和终点)发出 ...

  9. poj 1753 Flip Game 高斯消元

    题目链接 4*4的格子, 初始为0或1, 每次翻转一个会使它四周的也翻转, 求翻转成全0或全1最少的步数. #include <iostream> #include <vector& ...

随机推荐

  1. UDP广播

    客户端UDP发送消息至服务器端服务器IP:192.168.1.114服务器端口:2014 客户端 Socket socket = new Socket(AddressFamily.InterNetwo ...

  2. [Python] Format Strings in Python

    Single quotes and double quotes can both be used to declare strings in Python. You can even use trip ...

  3. CS224d lecture 9札记

    欢迎转载.转载注明出处: http://blog.csdn.net/neighborhoodguo/article/details/47193885 近期几课的内容不是非常难.还有我的理解能力有所提高 ...

  4. occActiveX - ActiveX with OpenCASCADE

    occActiveX - ActiveX with OpenCASCADE eryar@163.com Abstract. OpenCASCADE ActiveX wrapper for VB, C# ...

  5. android-从官网下拉源码(ubuntu)

    今天终于成功的从谷歌官网上下载了android 源码.中间折腾了好久,最终总算有所收获 1.下载repo curl https://storage.googleapis.com/git-repo-do ...

  6. 73.node.js开发错误——TypeError: Cannot set property 'XXX' of undefined

    转自:https://blog.csdn.net/fd214333890/article/details/53467429

  7. JS中的预解析

    js预解析对于很多学习web前端开发的新手们很困扰,总是很难搞懂到底是个什么东西,今天零度就为大家简单的分析一下,争取让大家都明白! 首先,看一下下面的代码: alert(a); var a = 1; ...

  8. 网络最大流算法—Dinic算法及优化

    前置知识 网络最大流入门 前言 Dinic在信息学奥赛中是一种最常用的求网络最大流的算法. 它凭借着思路直观,代码难度小,性能优越等优势,深受广大oier青睐 思想 $Dinic$算法属于增广路算法. ...

  9. 调用C#版gdal库的一个注意事项

    作者:朱金灿 来源:http://blog.csdn.net/clever101 在编译完C#版gdal库(x86平台)下,写了一个C#的控制台测试程序,出现下面的错误: 解决办法是将工程的目标平台设 ...

  10. 【DRF分页】

    目录 第一种 PageNumberPagination 查第n页,每页显示n条数据 第二种 LimitOffsetPagination 在第n个位置,向后查n条数据 第三种 CursorPaginat ...