poj 2947
来源:互联网 发布:淘宝注册怎么注册账号 编辑:程序博客网 时间:2024/06/02 02:11
题目描述:
就是求模7的方程组.然后结果在3到9之间.n~300
题解:
高斯消元.用公倍数和除以逆元来解.
重点:
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <ctype.h>#include <limits.h>#include <cstdlib>#include <algorithm>#include <vector>#include <queue>#include <map>#include <stack>#include <set>#include <bitset>#define CLR(a) memset(a, 0, sizeof(a))#define REP(i, a, b) for(int i = a;i < b;i++)#define REP_D(i, a, b) for(int i = a;i <= b;i++)typedef long long ll;using namespace std;const int maxn = 300+100;const int MOD=7;int m,n;int a[maxn][maxn], x[maxn];int equ, var;inline int gcd(int a,int b){ while(b != 0) { int t = b; b = a%b; a = t; } return a;}inline int lcm(int a,int b){ return a/gcd(a,b)*b;}long long inv(long long a,long long m){ if(a == 1)return 1; int t = pow(a, m-2) + 1e-9; return t%MOD;}int gauss(){ int k, col; for(k=0,col=0; k<equ&&col<var; k++,col++) { int max_r = k; for(int i = k+1; i < equ; i++) if(abs(a[i][col]) > abs(a[max_r][col])) max_r = i; if(a[max_r][col] == 0) { k--; continue; } if(max_r != k) for(int j = col; j < var+1; j++) swap(a[k][j],a[max_r][j]); for(int i=k+1; i<equ; i++) { if(a[i][col]!=0) { int d = lcm(abs(a[i][col]), abs(a[k][col]) ); int d_k = d/a[k][col], d_i = d/a[i][col]; if(a[i][col]*a[k][col]<0) d_i=-d_i; for(int j=col; j<=var; j++) { int lft = ((a[i][j]*d_i-a[k][j]*d_k)%MOD+MOD)%MOD; a[i][j]=lft; } } } } for(int i = k; i < equ; i++) if(a[i][col] != 0) return -1; if(k < var) return var-k; for(int i=var-1; i>=0; i--) { int temp = a[i][var]; for(int j=i+1; j<var; j++) { temp = ((temp-(a[i][j]*x[j])%MOD)%MOD+MOD)%MOD; } x[i] = (temp*inv(a[i][i],MOD))%MOD; } return 0;}void solve(){ equ=m; var=n; int t=gauss(); if(t==-1) { printf("Inconsistent data.\n"); } else if(t>0) { printf("Multiple solutions.\n"); } else { for(int i=0; i<var; i++) { if(x[i]<=2) x[i] += 7; printf("%d%c", x[i], (i==var-1 ? '\n' : ' ')); } }}int change(char s[]){ if(strcmp(s,"MON") == 0) return 1; else if(strcmp(s,"TUE")==0) return 2; else if(strcmp(s,"WED")==0) return 3; else if(strcmp(s,"THU")==0) return 4; else if(strcmp(s,"FRI")==0) return 5; else if(strcmp(s,"SAT")==0) return 6; else return 7;}int main(){ freopen("5Ein.txt", "r", stdin); //freopen("5Eout.txt", "w", stdout); while(scanf("%d%d",&n,&m) == 2) { if(n == 0 && m == 0)break; memset(a,0,sizeof(a)); char str1[10],str2[10]; int k; for(int i = 0; i < m; i++) { scanf("%d%s%s",&k,str1,str2); a[i][n] = ((change(str2) - change(str1) + 1)%MOD + MOD)%MOD; int t; while(k--) { scanf("%d",&t); t--; a[i][t] ++; a[i][t]%=MOD; } } solve(); } return 0;}
0 0
- poj 2947
- poj 2947 java
- poj 2947 Widget Factory
- POJ 2947 Widget Factory
- POJ 2947 Widget Factory
- POJ 2947 高斯消元
- Widget Factory POJ 2947
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- 毕业后的五年拉开大家差距的原因在哪里?
- c#第五次作业
- 排序算法之快速排序算法
- 【XML】XML,HTML,XHTML
- 大数据
- poj 2947
- 死锁
- Leetcode #17 Letter Combinations of a Phone Number
- 有道词典Demo(WebView)
- 在windows上编译scite
- 最后的最后的一场考试
- 又是一个安静的夜晚
- LeetCode之Valid Parentheses
- 字符串转换为十进制数(算法)