高斯消元解同余方程poj2947
来源:互联网 发布:信捷触摸屏怎么编程 编辑:程序博客网 时间:2024/04/30 00:51
Language:
Widget Factory
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 4689 Accepted: 1595
Description
The widget factory produces several different kinds of widgets. Each widget is carefully built by a skilled widgeteer. The time required to build a widget depends on its type: the simple widgets need only 3 days, but the most complex ones may need as many as 9 days.
The factory is currently in a state of complete chaos: recently, the factory has been bought by a new owner, and the new director has fired almost everyone. The new staff know almost nothing about building widgets, and it seems that no one remembers how many days are required to build each diofferent type of widget. This is very embarrassing when a client orders widgets and the factory cannot tell the client how many days are needed to produce the required goods. Fortunately, there are records that say for each widgeteer the date when he started working at the factory, the date when he was fired and what types of widgets he built. The problem is that the record does not say the exact date of starting and leaving the job, only the day of the week. Nevertheless, even this information might be helpful in certain cases: for example, if a widgeteer started working on a Tuesday, built a Type 41 widget, and was fired on a Friday,then we know that it takes 4 days to build a Type 41 widget. Your task is to figure out from these records (if possible) the number of days that are required to build the different types of widgets.
The factory is currently in a state of complete chaos: recently, the factory has been bought by a new owner, and the new director has fired almost everyone. The new staff know almost nothing about building widgets, and it seems that no one remembers how many days are required to build each diofferent type of widget. This is very embarrassing when a client orders widgets and the factory cannot tell the client how many days are needed to produce the required goods. Fortunately, there are records that say for each widgeteer the date when he started working at the factory, the date when he was fired and what types of widgets he built. The problem is that the record does not say the exact date of starting and leaving the job, only the day of the week. Nevertheless, even this information might be helpful in certain cases: for example, if a widgeteer started working on a Tuesday, built a Type 41 widget, and was fired on a Friday,then we know that it takes 4 days to build a Type 41 widget. Your task is to figure out from these records (if possible) the number of days that are required to build the different types of widgets.
Input
The input contains several blocks of test cases. Each case begins with a line containing two integers: the number 1 ≤ n ≤ 300 of the different types, and the number 1 ≤ m ≤ 300 of the records. This line is followed by a description of the m records. Each record is described by two lines. The first line contains the total number 1 ≤ k ≤ 10000 of widgets built by this widgeteer, followed by the day of week when he/she started working and the day of the week he/she was fired. The days of the week are given bythe strings `MON', `TUE', `WED', `THU', `FRI', `SAT' and `SUN'. The second line contains k integers separated by spaces. These numbers are between 1 and n , and they describe the diofferent types of widgets that the widgeteer built. For example, the following two lines mean that the widgeteer started working on a Wednesday, built a Type 13 widget, a Type 18 widget, a Type 1 widget, again a Type 13 widget,and was fired on a Sunday.
4 WED SUN
13 18 1 13
Note that the widgeteers work 7 days a week, and they were working on every day between their first and last day at the factory (if you like weekends and holidays, then do not become a widgeteer!).
The input is terminated by a test case with n = m = 0 .
4 WED SUN
13 18 1 13
Note that the widgeteers work 7 days a week, and they were working on every day between their first and last day at the factory (if you like weekends and holidays, then do not become a widgeteer!).
The input is terminated by a test case with n = m = 0 .
Output
For each test case, you have to output a single line containing n integers separated by spaces: the number of days required to build the different types of widgets. There should be no space before the first number or after the last number, and there should be exactly one space between two numbers. If there is more than one possible solution for the problem, then write `Multiple solutions.' (without the quotes). If you are sure that there is no solution consistent with the input, then write `Inconsistent data.'(without the quotes).
Sample Input
2 32 MON THU1 23 MON FRI1 1 23 MON SUN1 2 210 21 MON TUE 31 MON WED30 0
Sample Output
8 3Inconsistent data.
Hint
Huge input file, 'scanf' recommended to avoid TLE.
根据给出的条件可以列出方程
(a11x1+a12x2+...+a1nxn)%7=y1
(a21x1+a22x2+...+a2nxn)%7=y2
...
...
(an1x1+an2x2+...+annxn)%7=yn
然后高斯消元就行了
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=300;const int MOD=7;int N,M;char s1[10],s2[10];int equ,var;int num[maxn];int a[maxn][maxn],x[maxn];bool free_x[maxn];void init(){ equ=M,var=N; memset(a,0,sizeof(a)); memset(x,0,sizeof(x));}int gcd(int x,int y){ if(!y)return x; return gcd(y,x%y);}int lcm(int x,int y){ return x*y/gcd(x,y);}int Gauss(){ int row=0,col=0; int free_num=0; int LCM,ta,tb; int free_index,temp; for(int i=0;i<=var;i++)free_x[i]=true; for(;row<equ&&col<var;row++,col++) { int r=row; for(int i=row+1;i<equ;i++) if(abs(a[i][col])>abs(a[r][col]))r=i; if(row!=r) for(int i=row;i<=var;i++)swap(a[r][i],a[row][i]); if(!a[row][col]) { row--; continue; } for(int i=row+1;i<equ;i++) { if(a[i][col]!=0) { LCM=lcm(abs(a[i][col]),abs(a[row][col])); ta=LCM/abs(a[i][col]); tb=LCM/abs(a[row][col]); if(a[i][col]*a[row][col]<0)tb=-tb; for(int j=col;j<=var;j++) a[i][j]=((a[i][j]*ta-a[row][j]*tb)%MOD+MOD)%MOD; } } } for(int i=row;i<equ;i++) if(a[i][col]!=0)return -1; if(row<var) { for(int i=row-1;i>=0;i--) { free_num=0; for(int j=0;j<var;j++) if(a[i][j]!=0&&free_x[j])free_num++,free_index=j; if(free_num>1)continue; temp=a[i][var]; for(int j=0;j<var;j++) { if(a[i][j]!=0&&j!=free_index)temp-=a[i][j]*x[j]%MOD; temp=(temp%MOD+MOD)%MOD; } x[free_index]=(temp/a[i][free_index])%MOD; free_x[free_index]=0; } return var-row; } for(int i=var-1;i>=0;i--) { temp=a[i][var]; for(int j=i+1;j<var;j++) { if(a[i][j]!=0)temp-=a[i][j]*x[j]; temp=(temp%MOD+MOD)%MOD; } while(temp%a[i][i]!=0)temp+=MOD; x[i]=(temp/a[i][i])%MOD; } return 0;}int encode(char *s){ if(!strcmp(s,"MON"))return 1; else if(!strcmp(s,"TUE"))return 2; else if(!strcmp(s,"WED"))return 3; else if(!strcmp(s,"THU"))return 4; else if(!strcmp(s,"FRI"))return 5; else if(!strcmp(s,"SAT"))return 6; else return 7;}int main(){ int cnt,y; while(scanf("%d%d",&N,&M)!=EOF,N+M) { init(); for(int i=0;i<M;i++) { scanf("%d%s%s",&cnt,s1,s2); int p=(encode(s2)-encode(s1)+1+MOD)%MOD; memset(num,0,sizeof(num)); for(int j=0;j<cnt;j++) { scanf("%d",&y); num[y]++; } for(int j=1;j<=N;j++) a[i][j-1]=num[j]%MOD; a[i][N]=p; } int cnt=Gauss(); if(cnt==-1)printf("Inconsistent data.\n"); else if(cnt>0)printf("Multiple solutions.\n"); else { bool flag=true; for(int i=0;i<N;i++) { if(flag)flag=false; else printf(" "); if(x[i]<3)x[i]+=7; else if(x[i]>9)x[i]-=7; printf("%d",x[i]); } printf("\n"); } } return 0;}
0 0
- 高斯消元解同余方程poj2947
- POJ2947 高斯消元解同余方程
- POJ2947高斯消元+同余方程
- POJ2947 Widget Factory【高斯消元】【同余方程】
- poj 2497 高斯消元解同余方程
- 线性同余方程
- 解同余方程
- 线性同余方程
- 线性同余方程
- 线性同余方程
- 一次同余方程
- poj2115 同余方程
- 同余方程
- NOIP2012 同余方程
- 【noip2012】同余方程
- BZOJ2995同余方程
- NOIP2012 同余方程
- NOIP2012D2T1 同余方程
- 动态计算UITableViewCell高度详解
- CentOS 6.6 安装JDK7 && Tomcat7
- 正确使用Block避免Cycle Retain和Crash
- Xcode5.1及相关软件离线下载地址
- android中欢迎界面的实现
- 高斯消元解同余方程poj2947
- ALM通过测试集ID获取测试集
- 17、SPI实验
- 安卓apk文件反编译
- hdu 4340 树形dp+染色问题
- 1412201233-hd-EXCEL排序
- ALM通过测试集获取测试实例列表
- asp.net生成html静态页
- 财经法规与会计职业道德