Widget Factory
2016-05-30 08:48
211 查看
Language: Default Widget Factory
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. 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 . 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 3 2 MON THU 1 2 3 MON FRI 1 1 2 3 MON SUN 1 2 2 10 2 1 MON TUE 3 1 MON WED 3 0 0 Sample Output 8 3 Inconsistent data. Hint Huge input file, 'scanf' recommended to avoid TLE. Source Central Europe 2005 |
p start end
a1,a2......ap (1<=ai<=n)
第一行表示从星期start 到星期end 一共生产了p 件装饰物(工作的天数为end-start+1+7*x,
加7*x 是因为它可能生产很多周),第二行表示这p 件装饰物的种类(可能出现相同的种类,
即ai=aj)。规定每件装饰物至少生产3 天,最多生产9 天。问每种装饰物需要生产的天数。
如果没有解,则输出“Inconsistent data.”,如果有多解,则输出“Multiple solutions.”,如果
只有唯一解,则输出每种装饰物需要生产的天数。
思路:高斯消元;
我们可以根据题意写出方程组,(a1x1+a2x2+a3x3+...)%7=(k)%7;
然后高斯消元解方程组;最后结果为(akxk)%7=(s)%7;用扩展欧几里得求下即可;
复杂(n3)
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<map> using namespace std; typedef long long LL; map<string,LL>my; LL ju[400][400]; LL x[400]; bool ree[400]; char a[100]; char b[100]; LL gcd(LL n,LL m) { if(m==0) return n; else if(n%m==0) { return m; } else return gcd(m,n%m); } LL guss(LL equ,LL var,LL n); pair<LL ,LL>P(LL n,LL m); int main(void) { string aa; aa="MON"; my[aa]=1; aa="TUE"; my[aa]=2; aa="WED"; my[aa]=3; aa="THU"; my[aa]=4; aa="FRI"; my[aa]=5; aa="SAT"; my[aa]=6; aa="SUN"; my[aa]=7; LL i,j,k;LL n,m; while(scanf("%lld %lld",&n,&m),n!=0&&m!=0) { memset(ju,0,sizeof(ju)); for(j=0; j<m; j++) { scanf("%lld",&k); scanf("%s",a);scanf("%s",b); LL xx=my[a]; LL yy=my[b]; for(i=0; i<k; i++) { LL xc; scanf("%lld",&xc); xc--; ju[j][xc]++; ju[j][xc]%=7; } ju[j] =(yy-xx+1)%7; } LL akk=guss(m,n,n); if(akk==0) { for(i=0; i<n; i++) { LL uk=gcd(ju[i][i],7); if(ju[i] %uk) { akk=-1; break; } else { ju[i][i]/=uk; ju[i] /=uk; pair<LL,LL>cc=P(ju[i][i],7/uk); x[i]=((ju[i] *cc.first)%(7/uk)+7/uk)%(7/uk); while(x[i]<3) { x[i]+=7/uk; } if(x[i]>9||x[i]<3) { akk=-1; break; } } } } if(akk==-1) { printf("Inconsistent data.\n"); } else if(akk) { printf("Multiple solutions.\n"); } else { printf("%lld",x[0]); for(i=1; i<n; i++) printf(" %lld",x[i]); printf("\n"); } } return 0; } LL guss(LL equ,LL var,LL n) { LL i,j,k; LL max_r; LL col; for(i=0; i<=var; i++) { x[i]=0; } col=0; for(k=0; k<equ&&col<var; k++,col++) { max_r=k; for(i=k+1; i<equ; i++) { if(fabs(ju[i][col])>fabs(ju[max_r][col])) { max_r=i; } } if(i!=k) { for(j=col; j<var+1; j++) { swap(ju[max_r][j],ju[k][j]); } } if(ju[k][col]==0) { k--; continue; } for(i=0; i<equ; i++) { if(i!=k&&ju[i][col]!=0) { LL xt=gcd((LL)fabs(ju[i][col]),(LL)fabs(ju[k][col])); LL yt=fabs(ju[i][col])/xt*fabs(ju[k][col]); LL nn,mm; nn=yt/(LL)fabs(ju[k][col]); mm=yt/(LL)fabs(ju[i][col]); if(ju[i][col]*ju[k][col]<0) { nn=-nn; } for(j=0; j<=var; j++) { ju[i][j]=(((ju[i][j]%7)*(mm%7)-(nn%7)*(ju[k][j])%7)+7)%7; } } } } LL sum=0; LL flag=0; for(i=k;i<equ;i++) { if(ju[i] ) { flag=-1; return flag; } } if(k<n) flag=1; return flag; } pair<LL ,LL>P(LL n,LL m) { if(m==0) { pair<LL,LL>C=make_pair(1,0); return C; } else { pair<LL,LL>N=P(m,n%m); LL x=N.second; LL y=N.first; N.first=x; N.second=y-(n/m)*x; return N; } }
相关文章推荐
- 144. Binary Tree Preorder Traversal
- SQL Server2012创建连接服务器到ORACLE11G
- 个人工作总结14
- sqlserve2012链接服务器 测试不成功故障
- 人脸识别之人脸检测(八)--HOG特征原理及实现
- 全面解析Bootstrap中form、navbar的使用方法
- jstorm简介(转)
- 자동생성
- 飞花的鱼塘
- 【备战面试之】四、可空类型Nullable<T>到底是什么鬼
- Apache Mina Server 2.0 中文参考手册
- 开源 java CMS - FreeCMS2.4 站点设置
- OC 单例模式
- Android高效加载大图、多图解决方案,有效避免程序OOM
- 上下文菜单ContextMenu
- Nginx模块fastcgi_cache的几个注意点 转
- sprintf、strcpy和memcpy的区别
- 冒泡选择排序
- Win10免费升级超级优惠仅剩2个月 Win7/Win8.1用户抓紧升级
- 设计模式 - 工厂模式