Description
Miss Kitty graduated from University N that has a long history of training their students to become teachers in an island country T. All of her classmates, including Miss Kitty, work very hard at school to become teachers of primary or secondary schools. However, due to the extremely low birth rate in island T, there is little demand for teachers in the island T. Miss Kitty has changed her career plan after unable to secure a stable job in the teaching market after more than 5 years of struggling. She wants to acquire other practical skills by entering graduate schools. However, the tuition for higher education in island T is very high. She needs to save money before becoming a student again. She has opened an ice cream shop near University N. She saves all of the money she earned and will close the shop and enter a graduate institute of making ice creams after she has earned at least m dollars.
She works very hard every day by selling ice creams and opens the shop every day. Nearby customers become to love her ice cream day after day. After a few days, she discovered the following magic formulas about the amount of money she earned each day. Assume x is the number of days the shop is opened. So x = 1 for the first day the shop is opened.
The following is an example when a = 1 , b = 2 , c = 1 , d = 1 , e = - 2 , f = 1 , g = 0 , h = 1 and i = 1 :
x | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
x2 + 2x + 1 (sunny) | 4 | 9 | 16 | 25 | 36 | 49 | 64 |
x2 - 2x + 1 (cloudy) | 0 | 1 | 4 | 9 | 16 | 25 | 36 |
x + 1 (raining) | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
Each day is either a sunny, cloudy or raining day. Furthermore, she also discovers the following magic formulas between x and the weather.
Note that mod is the operator to find the reminder of integer division. The following is an example when j = 2 and k = 1 :
x | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
2x + 1 | 3 | 5 | 7 | 9 | 11 | 13 | 15 |
(2x + 1) mod 3 | 0 | 2 | 1 | 0 | 2 | 1 | 0 |
weather | sunny | raining | cloudy | sunny | raining | cloudy | sunny |
Please use the above formulas to calculate for Miss Kitty, the least integer x so that the total amount of money earned by her is at least m .
The following is an example when m = 100 using the same values for a, ... , k as above:
x | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
weather | sunny | raining | cloudy | sunny | raining | cloudy | sunny |
money earn today | 4 | 3 | 4 | 25 | 6 | 25 | 64 |
total money earned | 4 | 7 | 11 | 36 | 42 | 67 | 131 |
Hence the answer is x = 7 . Miss Kitty needs to open the ice cream for at least 7 days.
Technical Specification
Input
The first line of the input file contains an integer indicating the number of test cases to follow. Then each of the next line contains
m a b c d e f g h i j k
Output
For each test case, output the least x so that the total amount of money earned is at least m .
Sample Input
2 100 1 2 1 1 -2 1 0 1 1 2 1 1234 2 1 2 2 -2 0 1 -5 0 4 8
Sample Output
7 14
//Memory: 0 KB Time: 12 MS //Language: ANSI C 4.1.2 Result: Accepted #include <stdio.h> int main() { int m, a, b, c, d, e, f, g, h, i, j, k, x, sum, T; scanf("%d", &T); while(T--) { x=1;sum=0; scanf("%d%d%d%d%d%d%d%d%d%d%d%d", &m, &a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k); while(sum < m) { int w = (j*x + k)%3; if(!w) sum += (a*x*x + b*x + c); else if(w == 1) sum += (d*x*x + e*x + f); else sum += (g*x*x + h*x + i); x++; } printf("%d\n", x - 1); } return 0; }