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

  1. 2 3
  2. 2 MON THU
  3. 1 2
  4. 3 MON FRI
  5. 1 1 2
  6. 3 MON SUN
  7. 1 2 2
  8. 10 2
  9. 1 MON TUE
  10. 3
  11. 1 MON WED
  12. 3
  13. 0 0

Sample Output

  1. 8 3
  2. Inconsistent data.

Hint

Huge input file, 'scanf' recommended to avoid TLE.
 
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <cmath>
  5. using namespace std;
  6. const int maxn = ;
  7. int equ, var; // 有equ个方程,var个变元。增广阵行数为equ, 分别为0到equ - 1,列数为var + 1,分别为0到var.
  8. int a[maxn][maxn];//增广矩阵
  9. int x[maxn]; // 解集.
  10. int free_num;
  11.  
  12. inline int gcd(int a, int b)
  13. {
  14. int t;
  15. while(b!=)
  16. {
  17. t=b;
  18. b=a%b;
  19. a=t;
  20. }
  21. return a;
  22. }
  23.  
  24. inline int lcm(int a, int b)
  25. {
  26. return a*b/gcd(a,b);
  27. }
  28. // 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数)
  29. int change(char s[])
  30. {
  31. if(strcmp(s,"MON")==) return ;
  32. else if(strcmp(s,"TUE")==) return ;
  33. else if(strcmp(s,"WED")==) return ;
  34. else if(strcmp(s,"THU")==) return ;
  35. else if(strcmp(s,"FRI")==) return ;
  36. else if(strcmp(s,"SAT")==) return ;
  37. else return ;
  38. }
  39. int Gauss(void)
  40. {
  41. int i,j,k;
  42. int max_r; // 当前这列绝对值最大的行.
  43. int col; // 当前处理的列.
  44. int ta, tb;
  45. int LCM;
  46. int temp;
  47. // 转换为阶梯阵.
  48. col = ; // 当前处理的列.
  49. for (k = ; k < equ && col < var; k++, col++)
  50. { // 枚举当前处理的行.
  51. // 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)
  52. max_r = k;
  53. for (i = k + ; i < equ; i++)
  54. {
  55. if (abs(a[i][col]) > abs(a[max_r][col])) max_r = i;
  56. }
  57. if (max_r != k)
  58. { // 与第k行交换.
  59. for (j = k; j < var + ; j++) swap(a[k][j], a[max_r][j]);
  60. }
  61. if (a[k][col] == )
  62. { // 说明该col列第k行以下全是0了,则处理当前行的下一列.
  63. k--;
  64. continue;
  65. }
  66. for (i = k + ; i < equ; i++)
  67. { // 枚举要删去的行.
  68. if (a[i][col] != )
  69. {
  70. LCM = lcm(abs(a[i][col]), abs(a[k][col]));
  71. ta = LCM / abs(a[i][col]), tb = LCM / abs(a[k][col]);
  72. if (a[i][col] * a[k][col] < ) tb = -tb; // 异号的情况是两个数相加.
  73. for (j = col; j < var + ; j++)
  74. {
  75. a[i][j] =(((a[i][j] * ta - a[k][j] * tb)%+)%);
  76. }
  77. }
  78. }
  79. }
  80. //Debug();
  81. // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).
  82. for (i = k; i < equ; i++)
  83. { // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.
  84. if (a[i][col] != ) return -;
  85. }
  86. // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.
  87. // 且出现的行数即为自由变元的个数.
  88. if (k < var)
  89. return var - k; // 自由变元有var - k个.
  90. // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.
  91. // 计算出Xn-1, Xn-2 ... X0.
  92. for (i = var - ; i >= ; i--)
  93. {
  94. temp = a[i][var];//等式右边的数
  95. for (j = i + ; j < var; j++)
  96. {
  97. if (a[i][j] != ) temp -= a[i][j] * x[j];//把已知的解带入,减去,只剩下,一个未知的解
  98. temp=(temp%+)%;
  99. }
  100. while(temp%a[i][i]!=)//外层每次循环都是为了求 a[i][i],因为它是每个方程中唯一一个未知的变量(求该方程时)
  101. temp+=;//因为天数不确定,而a[i][i]必须得为整数才可以,周期为7
  102. x[i]=(temp/a[i][i])%;
  103. }
  104. return ;
  105. }
  106.  
  107. int main(void)
  108. {
  109. int n,m,k,num;
  110. char s[],e[];
  111. while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
  112. {
  113. memset(a,,sizeof(a));
  114. for(int i=;i<m;i++)
  115. {
  116. scanf("%d",&k);
  117. scanf("%s%s",s,e);
  118. a[i][n]=((change(e)-change(s)+)%+)%;
  119. for(int j=;j<=k;j++)//k是他打造的数量
  120. {
  121. scanf("%d",&num);//可能是相同的数
  122. num--;
  123. a[i][num]++;//系数++
  124. a[i][num]%=;//有重复的。
  125. }
  126. }
  127. equ=m;//有m个方程
  128. var=n;//有多少个变量
  129. free_num = Gauss();
  130. if(free_num==)
  131. {
  132. for(int i=;i<n;i++)//根据题意,每个零件的加工时间在3-9天.
  133. if(x[i]<=)
  134. x[i]+=;
  135. for(int i=;i<n-;i++)
  136. cout<<x[i]<<" ";
  137. cout<<x[n-]<<endl;
  138. }
  139. else if(free_num==-)
  140. cout<<"Inconsistent data."<<endl;
  141. else
  142. cout<<"Multiple solutions."<<endl;
  143. }
  144. return ;
  145. }

Widget Factory (高斯消元解线性方程组)的更多相关文章

  1. Poj 2947 widget factory (高斯消元解同模方程)

    题目连接: http://poj.org/problem?id=2947 题目大意: 有n种类型的零件,m个工人,每个零件的加工时间是[3,9],每个工人在一个特定的时间段内可以生产k个零件(可以相同 ...

  2. POJ2947Widget Factory(高斯消元解同模方程)

    http://poj.org/problem?id=2947 题目大意:有n 种装饰物,m 个已知条件,每个已知条件的描述如下:p start enda1,a2......ap (1<=ai&l ...

  3. POJ 2947-Widget Factory(高斯消元解同余方程式)

    题目地址:id=2947">POJ 2947 题意:N种物品.M条记录,接写来M行,每行有K.Start,End,表述从星期Start到星期End,做了K件物品.接下来的K个数为物品的 ...

  4. 题解【AcWing883】高斯消元解线性方程组

    题面 高斯消元模板题. 这里直接讲述一下高斯消元的算法流程: 枚举每一列 \(c\): 找到第 \(c\) 列绝对值最大的一行: 将这一行换到最上面: 将该行的第一个数变成 \(1\): 将下面所有行 ...

  5. POJ 2947 2947 Widget Factory 高斯消元

    给出组件的数量n,给出记录的数量m(n就是变元数量,m是方程数量).每一个记录代表一个方程,求每个组件的生产天数. 高斯消元即可 #include <cstdio> #include &l ...

  6. bzoj千题计划187:bzoj1770: [Usaco2009 Nov]lights 燈 (高斯消元解异或方程组+枚举自由元)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1770 a[i][j] 表示i对j有影响 高斯消元解异或方程组 然后dfs枚举自由元确定最优解 #in ...

  7. [置顶] hdu 4418 高斯消元解方程求期望

    题意:  一个人在一条线段来回走(遇到线段端点就转变方向),现在他从起点出发,并有一个初始方向, 每次都可以走1, 2, 3 ..... m步,都有对应着一个概率.问你他走到终点的概率 思路: 方向问 ...

  8. 【BZOJ】2466: [中山市选2009]树 高斯消元解异或方程组

    [题意]给定一棵树的灯,按一次x改变与x距离<=1的点的状态,求全0到全1的最少次数.n<=100. [算法]高斯消元解异或方程组 [题解]设f[i]=0/1表示是否按第i个点的按钮,根据 ...

  9. 【高斯消元解xor方程】BZOJ1923-[Sdoi2010]外星千足虫

    [题目大意] 有n个数或为奇数或为偶数,现在进行m次操作,每次取出部分求和,告诉你这几次操作选取的数和它们和的奇偶性.如果通过这m次操作能得到所有数的奇偶性,则输出进行到第n次时即可求出答案:否则输出 ...

随机推荐

  1. 转:SAX解析的characters方法被多次调用

    原文地址:https://blog.csdn.net/liuxiaoddd/article/details/28885177 android SAX解析的characters方法被多次调用 同理 试用 ...

  2. 树和二叉树->线索二叉树

    文字描述 从二叉树的遍历可知,遍历二叉树的输出结果可看成一个线性队列,使得每个结点(除第一个和最后一个外)在这个线形队列中有且仅有一个前驱和一个后继.但是当采用二叉链表作为二叉树的存储结构时,只能得到 ...

  3. Java如何对List集合的操作方法(一)

    目录: list中添加,获取,删除元素: list中是否包含某个元素: list中根据索引将元素数值改变(替换): list中查看(判断)元素的索引: 根据元素索引位置进行的判断: 利用list中索引 ...

  4. AT2567 RGB Sequence dp

    正解:计数dp 解题报告: 传送门! umm其实我jio得dp的题目的话就难在思想昂,,,知道状态知道转移就不难辣QAQ 所以就不说别的了直接写下思路放下代码就over辣QAQ 最基础的思想就是f[i ...

  5. HTTP协议属于应用层,而SOCKS协议属于传输层

    HTTP协议属于应用层,而SOCKS协议属于传输层 SOCKS代理 SOCKS代理能在任何端口,任何协议下运行. SOCKS V4只支持 TCP连接,而SOCKS V5在其基础上增加了安全认证以及对U ...

  6. 将gitlab中的postgresql数据库开通远程访问

    postgresql数据库是gitlab的一个配置数据库,记录gitlab的一些配置信息. 我们访问gitlab中的postgresql数据有本地命令行访问和远程可视化软件访问2种方式. (一)本地命 ...

  7. bat、sh等批处理文件(脚本文件)

    批处理文件(batch file):也被称为批处理程序或脚本,可以简化日常或重复性任务.本质是无格式的文本文件,它包含一条或多条命令.(1).bat是dos下的批处理文件,在window系统上执行的文 ...

  8. 生成器-代码举例:()和yield

    怎么自定义一个生成器:两个方法: 1.小括号包裹表达式 2.函数中用yield返回 方法一:①小括号包裹表达式 G=(x*2 for x in range(5)) print(G)输出:<gen ...

  9. Request实例

    Request常用方法        getRequestURL方法返回客户端发出请求时的完整URL. getRequestURI方法返回请求行中的资源名部分. getQueryString 方法返回 ...

  10. 【EatBook】-NO.2.EatBook.2.JavaArchitecture.1.001-《修炼Java开发技术在架构中体验设计模式和算法之美》-

    1.0.0 Summary Tittle:[EatBook]-NO.2.EatBook.2.JavaArchitecture.1.001-<修炼Java开发技术在架构中体验设计模式和算法之美&g ...