最终代码地址:https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1099.c
做这题的时候查了别人的做法花了半天都没搞明白怎么做的,我认为别的博客写的难以让人理解所以就造了这个轮子。
题目:
1099. Packing Passengers
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
PTA, Pack ‘em Tight Airlines is attempting the seemingly impossible—to fly with only full planes and still make a profit. Their strategy is simplicity and efficiency. Their fleet consists of 2 types of equipment (airline lingo for airplanes). Type A aircraft cost costA dollars to operate per flight and can carry passengersA passengers. Type B aircraft cost costB dollars to operate per flight and can carry passengersB passengers.
PTA has been using software that works well for fewer than 100 passengers, but will be far too slow for the number of passengers they expect to have with larger aircraft. PTA wants you to write a program that fills each aircraft to capacity (in keeping with the name Pack ‘em Tight) and also minimizes the total cost of operations for that route.
Input
The input file may contain data sets. Each data set begins with a line containing the integer n (1 <= n <= 2,000,000,000) which represents the number of passengers for that route. The second line contains costA and passengersA, and the third line contains costB and passengersB. There will be white space between the pairs of values on each line. Here, costA, passengersA, costB, and passengersB are all nonnegative integers having values less than 2,000,000,001.
After the end of the final data set, there is a line containing “0” (one zero) which should not be processed.
Output
For each data set in the input file, the output file should contain a single line formatted as follows:
Data set <N>: <A> aircraft A, <B> aircraft B
Where <N> is an integer number equal to 1 for the first data set, and incremented by one for each subsequent data set, <A> is the number of airplanes of type A in the optimal solution for the test case, and <B> is the number of airplanes of type B in the optimal solution. The ‘optimal‘ solution is a solution that lets PTA carry the number of passengers specified in the input for that data set using only airplanes loaded to their full capacity and that minimizes the cost of operating the required flights. If multiple alternatives exist fitting this description, select the one that uses most airplanes of type A. If no solution exists for PTA to fly the given number of passengers, the out line should be formatted as follows:
Data set <N>: cannot be flown
Sample Input
600 30 20 20 40 550 1 13 2 29 549 1 13 2 29 2000000000 1 2 3 7 599 11 20 22 40 0
Sample Output
Data set 1: 0 aircraft A, 15 aircraft B Data set 2: 20 aircraft A, 10 aircraft B Data set 3: 11 aircraft A, 14 aircraft B Data set 4: 6 aircraft A, 285714284 aircraft B Data set 5: cannot be flown
题意就是求出passenger_A * x + passenger_B * y = passengers, 使得cost_A * x + cost_B * y最小。
我起初的想法是算出A和B哪个性价比大,然后取性价比大的那个最大的可能,再逐步递减到能够整除为止,这样做的效率是0.05s。
代码如下:
1 // Problem#: 1099 2 // Submission#: 4376506 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University 6 #include <cstdio> 7 8 inline int fix_upper(int upper, int passengers, int passenger_upper, int passenger_others) { 9 int temp = passengers - upper * passenger_upper; 10 while (temp % passenger_others) { 11 if (upper == 0) 12 return -1; 13 upper--, temp += passenger_upper; 14 } 15 return upper; 16 } 17 18 int main() { 19 int passengers; 20 int cost_A, passenger_A, cost_B, passenger_B; 21 int upper, others; 22 int count = 1; 23 while (scanf("%d", &passengers) && passengers) { 24 scanf("%d %d", &cost_A, &passenger_A); 25 scanf("%d %d", &cost_B, &passenger_B); 26 if (passenger_A == 0 && passenger_B == 0) { 27 printf("Data set %d: cannot be flown\n", count++); 28 continue; 29 } 30 if ((passenger_A == 0 && passenger_B != 0) || (cost_A == 0 && cost_B == 0 && passenger_B > passenger_A)) { 31 if (passengers % passenger_B) 32 printf("Data set %d: cannot be flown\n", count++); 33 else 34 printf("Data set %d: %d aircraft A, %d aircraft B\n", count++, 0, passengers / passenger_B); 35 continue; 36 } 37 if ((passenger_B == 0 && passenger_A != 0) || (cost_A == 0 && cost_B == 0 && passenger_A > passenger_B)) { 38 if (passengers % passenger_A) 39 printf("Data set %d: cannot be flown\n", count++); 40 else 41 printf("Data set %d: %d aircraft A, %d aircraft B\n", count++, passengers / passenger_A, 0); 42 continue; 43 } 44 if (double(cost_A) / double(passenger_A) <= double(cost_B) / double(passenger_B)) { 45 upper = passengers / passenger_A; 46 upper = fix_upper(upper, passengers, passenger_A, passenger_B); 47 others = (passengers - upper * passenger_A) / passenger_B; 48 if (upper != -1) 49 printf("Data set %d: %d aircraft A, %d aircraft B\n", count++, upper, others); 50 else 51 printf("Data set %d: cannot be flown\n", count++); 52 } else { 53 upper = passengers / passenger_B; 54 upper = fix_upper(upper, passengers, passenger_B, passenger_A); 55 others = (passengers - upper * passenger_B) / passenger_A; 56 if (upper != -1) 57 printf("Data set %d: %d aircraft A, %d aircraft B\n", count++, others, upper); 58 else 59 printf("Data set %d: cannot be flown\n", count++); 60 } 61 } 62 return 0; 63 }
写的很丑请见谅,我只是为了AC,我知道这样做并不好当时。
然后就来讲一下更好的做法:
我们要求的是x, y满足passenger_A * x + passenger_B * y = passengers。
拓展欧几里德算法其实就是在欧几里德算法求解过程中把x和y算出来了,具体是这样的:a * x + b * y = gcd(a, b)。
其实就是把上面等式的x和y求出来了,原理如下:
我们知道欧几里德算法原理是gcd(a, b) = gcd(b, a % b)不断递归下去,拓展欧几里德算法其实也是这个递归,只不过多加了一些x和y的赋值罢了。
假设在某一次递归过程中,a‘ = b, b‘ = a % b = a - a / b * b(C语言整数算法)。
那么gcd(a, b) = gcd(a‘, b‘) = a‘x + b‘y。
消去a‘, b‘得到:ay +b(x - a / b * y) = Gcd(a, b)。
可以看到,这里的系数a的系数为y,b的系数为x - a / b * y。
通过这个原理递归我们最终得到的x和y就满足a * x + b * y = gcd(a, b)。
好,但是从这个算法也看不到和我们题目的关系是不是?
来看,首先有:passenger_A * x + passenger_B * y = gcd(passenger_A, passenger_B)
我们把上面的式子同时乘于passengers / gcd(passenger_A, passenger_B)试试:
passenger_A * (x * passengers / gcd(passenger_A, passenger_B)) + passenger_B * (y * passengers / gcd(passenger_A, passenger_B)) = passengers。
这个式子是不是就和passenger_A * x + passenger_B * y = passengers吻合了?
对应的x为(x * passengers / gcd(passenger_A, passenger_B)), y为(y * passengers / gcd(passenger_A, passenger_B))。
所以:我们先用欧几里德算法求出passenger_A * x + passenger_B * y = gcd(passenger_A, passenger_B)的x, y。
然后在通过变换得到passenger_A * x + passenger_B * y = passenger的x, y。
现在设这里求得的x, y为x0, y0。
这样就得到了满足以上等式一组的x0和y0的解了。
然后来看线型同余方程:
在数论中,线性同余方程是最基本的同余方程,“线性”表示方程的未知数次数是一次,即形如:
的方程。此方程有解当且仅当 b 能够被 a 与 n 的最大公约数整除(记作 gcd(a,n) | b)。这时,如果 x0 是方程的一个解,那么所有的解可以表示为:
其中 d 是a 与 n 的最大公约数。在模 n 的完全剩余系 {0,1,…,n-1} 中,恰有 d 个解。
所以,如果passengers不能整除gcd(passenger_A, passenger_B)则式子是无解的,若有解:
x = x0 + (passenger_B / gcd(passenger_A, passenger_B)) * k, y = y0 - (passenger_A / gcd(passenger_A, passenger_B)) * k, k为任意整数。
然后,根据题意,x >= 0, y >= 0。
所以有:
x0 + (passenger_B / gcd(passenger_A, passenger_B)) * k >= 0
=>
k >= (-x0) / (passenger_B / gcd(passenger_A, passenger_B))
y0 - (passenger_A / gcd(passenger_A, passenger_B)) * k >= 0
=>
k <= (y0) / (passenger_A / gcd(passenger_A, passenger_B))。
所以k的范围为:[(-x0) / (passenger_B / gcd(passenger_A, passenger_B)), (y0) / (passenger_A / gcd(passenger_A, passenger_B))]
然后来看:
我们的目标是cost_A * x + cost_B * y最小,带入x和y的表达式得:
cost_A * (x0 + (passenger_B / gcd(passenger_A, passenger_B)) * k) + cost_B * (y0 - (passenger_A / gcd(passenger_A, passenger_B)) * k)
消去x0, y0, gcd无关因素影响, 有:
cost_A * passenger_B * k - cost_B * passenger_A * k = k * (cost_A * passenger_B - cost_B * passenger_A)
好,那么就很简单了,如果 (cost_A * passenger_B - cost_B * passenger_A)为负,则k取最大值即可。
如果为正,k取最小值即可。然后得到k的值带入求出对应x和y即可。
代码如下:
1 #include <cstdio> 2 #include <cmath> 3 4 inline long long gcd_extend(int &a, int b, long long *x, long long *y) { 5 static long long r, t; 6 if (b == 0) { 7 *x = 1, *y = 0; 8 return a; 9 } else { 10 r = gcd_extend(b, a % b, x, y); 11 t = *x; 12 *x = *y; 13 *y = t - a / b * *y; 14 return r; 15 } 16 } 17 18 int main() { 19 int passengers; 20 int cost_A, passenger_A, cost_B, passenger_B; 21 int count = 1; 22 long long lower, upper, k, gcd, x, y; 23 while (scanf("%d", &passengers) && passengers) { 24 scanf("%d %d", &cost_A, &passenger_A); 25 scanf("%d %d", &cost_B, &passenger_B); 26 gcd = gcd_extend(passenger_A, passenger_B, &x, &y); 27 if (passengers % gcd == 0) { 28 x *= passengers / gcd; 29 y *= passengers / gcd; 30 upper = floor((double)y / (passenger_A / gcd)); 31 lower = ceil((double)-x / (passenger_B / gcd)); 32 k = passenger_B * cost_A - passenger_A * cost_B <= 0 ? upper : lower; 33 printf("Data set %d: %lld aircraft A, %lld aircraft B\n", count++, x + (passenger_B / gcd) * k, y - (passenger_A / gcd) * k); 34 } else 35 printf("Data set %d: cannot be flown\n", count++); 36 } 37 return 0; 38 }
这种做法效率为0.02s。