poj2947 高斯消元
Widget Factory
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 5218 | Accepted: 1802 |
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 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.
题意:
每个工人都有工作纪律,可以知道他做过哪些项目,总共用时多少
老板想知道每个项目要花费多少时间
思路:
因为你只知道开始和结束时间,并不知中经过了多少周,可以转化成方程组对mod取模套用即可,在得出x处,如果答案小于3,则要进行修改,要求的是在3—9天,WR了很久,最后发现是因为周二单词的缩写弄错了TAT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #include <functional> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <Map> using namespace std; typedef long long ll; typedef long double ld; using namespace std; const int maxn = 305; int equ,var; int a[maxn][maxn]; int x[maxn]; int free_x[maxn]; int free_num; void debug() { for ( int i = 0; i < equ; i++) { for ( int j = 0; j <= var; j++) printf ( "%d " ,a[i][j]); printf ( "\n" ); } } int gcd( int a, int b) { while (b) { int tmp = b; b = a%b; a = tmp; } return a; } int lcm( int a, int b) { return a/gcd(a,b)*b; } int Gauss( int mod) { int max_r,col,k; free_num = 0; for (k = 0,col = 0; k < equ && col < var; k++,col++) { 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 --; free_x[free_num++] = col ; 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 LCM = lcm( abs (a[i][col]), abs (a[k][col])); int ta = LCM / abs (a[i][col]); int tb = LCM / abs (a[k][col]); if (a[i][col] * a[k][col] < 0) tb = -tb; for ( int 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][col] != 0) return -1; if (k < var) return var-k; for ( int i = var-1; i >= 0; i--) { ll temp = a[i][var]; for ( int j = i +1; j < var; j++) temp =((temp- a[i][j]*x[j])%mod+mod)%mod; while (temp % a[i][i]) { temp += mod; } temp /= a[i][i]; temp %= 7; if (temp < 3) temp += 7; x[i] = temp; } return 0; } int n,m; void ini() { memset (a,0, sizeof (a)); memset (x,0, sizeof (x)); equ = m; var = n; } map<string, int >mp; void get() { mp[ "MON" ] = 1; mp[ "TUE" ] = 2; mp[ "WED" ] = 3; mp[ "THU" ] = 4; mp[ "FRI" ] = 5; mp[ "SAT" ] = 6; mp[ "SUN" ] = 7; } string star; string en; int main() { int k; get(); while ( scanf ( "%d%d" ,&n,&m) != EOF) { if (!n && !m) break ; ini(); for ( int i = 0; i < m; i++) { cin>>k>>star>>en; int day = mp[en] - mp[star]+1; if (day < 0) day += 7; a[i][n] =day; for ( int j = 0; j < k; j++) { int x; scanf ( "%d" ,&x); a[i][x-1] ++; } for ( int j = 0; j < n; j++) { a[i][j] %= 7; } } //debug(); int ans = Gauss(7); if (ans == -1) { printf ( "Inconsistent data.\n" ); } else if (ans > 0) printf ( "Multiple solutions.\n" ); else { for ( int i = 0; i < var; i ++ ) { printf ( "%d%c" , x[i], i == var-1 ? '\n' : ' ' ); } } } return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步