UVALive 4033

A - Miss Kitty and Her Little Ice Cream Shop
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

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.

 

  • During a sunny day, her shop earns exactly a . x2 + b . x + c dollars.
  • During a cloudy day, her shop earns exactly d . x2 + e . x + f dollars.
  • During a raining day, her shop earns exactly g . x2 + h . x + i dollars.

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.

 


  • If (j . x + k) can be evenly divided by 3, i.e., (j . x + k) mod 3 = 0, then it is a sunny day.
  • If (j . x + k) mod 3 = 1, then it is a cloudy day.
  • If (j . x + k) mod 3 = 2, then it is a rainy day.

 


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

 

  1. 0 < m10, 000, 000 and m is an integer
  2. -10a, b, c, d, e, f, g, h, i, j, k10 , and they are all integers

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

//题目好长的水题
#include
<iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <cmath> using namespace std; int a,b,c,d,e,f,g,h,i,j,k; int del(int x) { if(((x*j)+k)%3==0) { return a*x*x+b*x+c; } else if(((x*j)+k)%3==1) { return d*x*x+e*x+f; } else { return g*x*x+h*x+i; } } int main() { int m; int t; int kk; scanf("%d",&t); while(t--) { kk=0; scanf("%d",&m); scanf("%d%d%d%d%d%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f,&g,&h,&i,&j,&k); // printf("%d ",m); while(++kk) { m-=del(kk); if(m<=0) break; } printf("%d\n",kk); } }

你可能感兴趣的:(live)