http://poj.org/problem?id=2947
血泪史:
CE:poj的string类型要加string库,swap不能直接交换数组
WA:
x[m-1]也有可能<3啊O(≧口≦)O
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int mod=7; int n,m; char ch[7][4] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"}; int a[301][301]; int x[301]; int inv[10001]; void read(int &x) { x=0; char c=getchar(); while(!isdigit(c)) c=getchar(); while(isdigit(c)) { x=x*10+c-‘0‘; c=getchar(); } } int getgcd(int a,int b) { return !b ? a : getgcd(b,a%b); } int getlcm(int a,int b) { return a*b/getgcd(a,b); } void preinv() { inv[1]=1; for(int i=2;i<=10000;++i) inv[i]=(-mod/i*inv[mod%i]%mod+mod)%mod; } int gauss() { int equ=n,var=m; int i,j,k; int max_r,col; int ta,tb,lcm; int tmp; for(k=0,col=0;k<equ && col<var;++k,++col) { max_r=k; for(i=k+1;i<equ;++i) if(abs(a[i][col])>abs(a[max_r][col])) max_r=i; if(!a[max_r][col]) { --k; continue; } if(k!=max_r) for(j=col;j<var+1;++j) swap(a[k][j],a[max_r][j]); for(i=k+1;i<equ;++i) if(a[i][col]) { lcm=getlcm(abs(a[i][col]),abs(a[k][col])); ta=lcm/abs(a[i][col]); tb=lcm/abs(a[k][col]); if(a[i][col]*a[k][col]<0) tb=-tb; for(j=col;j<var+1;++j) a[i][j]=((a[i][j]*ta-a[k][j]*tb)%mod+mod)%mod; } } for(int i=k;i<equ;++i) if(a[i][var]) return -1; if(k<var) return -2; for(int i=var-1;i>=0;--i) { tmp=a[i][var]; for(j=i+1;j<var;++j) if(a[i][j]) { tmp-=a[i][j]*x[j]; tmp=(tmp%mod+mod)%mod; } x[i]=tmp*inv[a[i][i]]%mod; } return 0; } int turn(char *c) { for(int i=0;i<7;++i) if(!strcmp(c,ch[i])) return i+1; } int main() { int k,xi; char c[4]; int s,t; preinv(); while(scanf("%d%d",&m,&n)!=EOF) { if(!m) return 0; memset(a,0,sizeof(a)); for(int i=0;i<n;++i) { read(k); scanf("%s",c); s=turn(c); scanf("%s",c); t=turn(c); a[i][m]=(t-s+7+1)%7; while(k--) { read(xi); a[i][xi-1]++; } } for(int i=0;i<n;++i) for(int j=0;j<m;++j) a[i][j]%=mod; int ans=gauss(); if(ans==-1) puts("Inconsistent data."); else if(ans==-2) puts("Multiple solutions."); else { for(int i=0;i<m;++i) { if(x[i]<3) x[i]+=7; printf("%d%c",x[i],i==m-1 ? ‘\n‘ : ‘ ‘); } } } }
原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8186019.html
时间: 2024-10-09 14:35:04