POJ2947 DAZE [Gauss]
题目是要求建立一个方程组:
(mat[1][1]*x[1] + mat[1][2]*x[2] + … + mat[1][n]*x[n])%7 =mat[1][n+1]
(mat[2][1]*x[1] + mat[2][2]*x[2] + … + mat[2][n]*x[n])%7 =mat[2][n+1]
…
…
(mat[m][1]*x[1] + mat[m][2]*x[2] + … + mat[m][n]*x[n])%7 =mat[m][n+1]
假设有解输出解得个数。假设无解Inconsistent data.无穷多组解Multiple solutions.
扯一句,什么时候无解?
系数矩阵的秩 不等于 增广矩阵的秩 时。反映在程序上就是:
for (i = row; i < N; i++)
{
if (a[i][M] != 0)
{
printf("Inconsistent data.\n");
}
}
什么时候无穷多解?
当增广矩阵的秩小于行列式的行数的时候。 反映在程序上就是:
if (row < M)
{
printf("Multiple solutions.\n");
}
好了。程序例如以下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
using namespace std;
int m,n;//n lines
int M,N;
int a[333][333];
int times[333];
int ans[333];
int MOD=7;
int getday(char* s)
{
if(strcmp(s,"MON")==0) return 1;
if(strcmp(s,"TUE")==0) return 2;
if(strcmp(s,"WED")==0) return 3;
if(strcmp(s,"THU")==0) return 4;
if(strcmp(s,"FRI")==0) return 5;
if(strcmp(s,"SAT")==0) return 6;
if(strcmp(s,"SUN")==0) return 7;
} int extend_gcd(int A, int B, int &x, int &y)
{
if (B == 0)
{
x = 1, y = 0;
return A;
}
else
{
int r = extend_gcd(B, A%B, x, y);
int t = x;
x = y;
y = t - A / B*y;
return r;
}
} int lcm(int A, int B)
{
int x = 0, y = 0;
return A*B / extend_gcd(A, B, x, y);
}
void Guass()
{
int i, j, row, col;
for (row = 0, col = 0; row < N && col < M; row++, col++)
{
for (i = row; i < N; i++)
if (a[i][col]) break;
if (i == N)
{
row--;
continue;
}
if (i != row)
for (j = 0; j <= M; j++) swap(a[row][j], a[i][j]);
for (i = row + 1; i < N; i++)
{
if (a[i][col])
{
int LCM = lcm(a[row][col], a[i][col]);//利用最小公倍数去化上三角
int ch1 = LCM / a[row][col], ch2 = LCM / a[i][col];
for (j = col; j <= M; j++)
a[i][j] = ((a[i][j] * ch2 - a[row][j] * ch1)%MOD + MOD)%MOD;
}
}
}
for (i = row; i < N; i++)//无解
{
if (a[i][M] != 0)
{
printf("Inconsistent data.\n");
return;
}
}
if (row < M)//无穷多解
{
printf("Multiple solutions.\n");
return;
}
//唯一解时
for (i = M - 1; i >= 0; i--)
{
int ch = 0;
for (j = i + 1; j < M; j++)
{
ch = (ch + ans[j] * a[i][j] % MOD)%MOD;
}
int last = ((a[i][M] - ch)%MOD + MOD)%MOD;
int x = 0, y = 0;
int d = extend_gcd(a[i][i], MOD, x, y);
x %= MOD;
if (x < 0) x += MOD;
ans[i] = last*x / d%MOD;
if (ans[i] < 3) ans[i] += 7;
}
for (int i = 0; i < M; i++)
{
if (i == 0)
printf("%d", ans[i]);
else
printf(" %d", ans[i]);
}
printf("\n");
} int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==0&&n==0)
break;
M=m,N=n;
memset(a,0,sizeof(a));
memset(times,0,sizeof(times));
memset(ans,0,sizeof(ans));
for(int i=0;i<n;i++)
{
int prodnum;char str1[11],str2[11];
scanf("%d%s%s",&prodnum,str1,str2);
for(int j=0;j<prodnum;j++)
{
int tmp;
scanf("%d",&tmp);
a[i][tmp-1]++;
a[i][tmp-1]%=MOD;
}
a[i][m]=(getday(str2)-getday(str1)+1+MOD)%MOD;
}
Guass();
}
return 0;
}
POJ2947 DAZE [Gauss]的更多相关文章
- 高斯消元 & 线性基【学习笔记】
高斯消元 & 线性基 本来说不写了,但还是写点吧 [update 2017-02-18]现在发现真的有好多需要思考的地方,网上很多代码感觉都是错误的,虽然题目通过了 [update 2017- ...
- [Gauss]POJ2947 Widget Factory
题意: 有n种小工具要加工,每种工具的加工时间为3到9天,给了m条加工记录. 每条记录 X $s_1$ $s_2$ 分别代表 这个工人在$s_1$到$s_2$(前闭后闭)的时间里加工了X件小工具 ...
- hdu 5755(Gauss 消元) &poj 2947
Gambler Bo Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tota ...
- (模板)poj2947(高斯消元法解同余方程组)
题目链接:https://vjudge.net/problem/POJ-2947 题意:转换题意后就是已知m个同余方程,求n个变量. 思路: 值得学习的是这个模板里消元用到lcm的那一块.注意题目输出 ...
- OPEN CASCADE Gauss Least Square
OPEN CASCADE Gauss Least Square eryar@163.com Abstract. The least square can be used to solve a set ...
- OpenCASCADE Gauss Integration
OpenCASCADE Gauss Integration eryar@163.com Abstract. Numerical integration is the approximate compu ...
- C# 列主元素(Gauss)消去法 计算一元多次方程组
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C# 顺序高斯(Gauss)消去法计算一元多次方程组
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 整数矩阵CMO 2102回馈(gauss整数解)
PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘.目前又不当COO,还是得用心记代码哦! 本题是CMO(数学 Olympics) 2012 第二题 所以还是很坑的…… ...
随机推荐
- Swift,集合
1.创建(Set)集合(无序不可重复) (1)创建空集合 var a=Set<Int>() //[] (2)创建集合 var a:Set=[1,2,3] //[2,3,1] 2.集合插入( ...
- C语言中的联合体union所占内存方式
当多个数据需要共享内存或者多个数据每次只取其一时,可以利用联合体(union).在C Programming Language 一书中对于联合体是这么描述的: 1)联合体是一个结构: ...
- html DOM 的继承关系
零散的知识聚合在一起,就会形成力量,就有了生命力. 如各种语言的开发框架, 都是右各个碎片化的功能聚合在一起,构成有机地整体,便有了强大的力量.will be powerful! 如: jquery ...
- centos7 minimal connect: Network is unreachable(转)
新装的centos7,果然是很崭新啊. 装好之后打算看一下局域网的地址,然后就ip addr(centos 7 已经去掉了ifconfig这个命令).并没有显示局域网的ip地址. 然后我尝试ping ...
- 最小生成树之Prim(普里姆)算法
关于什么是Prim(普里姆算法)? 在实际生活中,我们常常碰到类似这种一类问题:如果要在n个城市之间建立通信联络网, 则连通n个城市仅仅须要n-1条线路.这时.我们须要考虑这样一个问题.怎样在最节省经 ...
- 自己亲自写的两本linux资料,免费下载,pdf文档
第一本是我写的韩顺平老师解说的linux视频的笔记,该视频原本有21讲.可是我始终没有找到当中的17.18讲.可是其它部分我感觉及记录的还是蛮认真的.该套视频解说的非常基础,因此我的这本笔记也非常基础 ...
- JAVA加解密 -- 数字签名算法
数字签名 – 带有密钥的消息摘要算法 作用:验证数据完整性.认证数据来源.抗否认(OSI参考模型) 私钥签名,公钥验证 RSA 包含非对称算法和数字签名算法 实现代码: //1.初始化密钥 KeyPa ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-点动面板的每个按钮含义
参考下面的图示 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123 我的在线论坛: http://csrobot.g ...
- Unity3D教程宝典之Web服务器篇:(第一讲)服务器的架设
转载自风宇冲Unity3D教程学院 引言:本文主要介绍WAMP服务器的架设. 第一部分WAMP介绍;第二部分WAMP安装及使用. 第一部分WAMP介绍 什 ...
- Androidproject师进阶之路 :《Android开发进阶:从小工到专家》上市啦!
封面 文件夹1 文件夹2 - 当当购买链接 - 京东购买链接 为什么写这本书 写这本书的念头由来已久了. 或许是从我打算写<Android源代码设计模式解析与实战>那时起就萌生了这个念头, ...