一开始TLE了。。。
1 /* 2 模拟 3 */ 4 #include<stdio.h> 5 #include<math.h> 6 const int maxn = 10005; 7 typedef __int64 int64; 8 int64 a[ maxn ]; 9 int main(){ 10 int64 k; 11 int n,k1,k2 ; 12 int T; 13 scanf("%d",&T); 14 int ca = 1; 15 while( T-- ){ 16 scanf("%d%d%d%I64d",&n,&k1,&k2,&k); 17 int64 sum = 0; 18 for( int i=0;i<n;i++ ){ 19 scanf("%I64d",&a[ i ]); 20 sum += a[ i ]; 21 } 22 int64 cnt = 0; 23 int flag = -1; 24 while( 1 ){ 25 if( sum>k ) { 26 flag = 1; 27 break; 28 }//ok 29 if( abs(k1+k2)<=1 ){ 30 flag = -1; 31 break; 32 } 33 int64 temp = a[ 0 ]; 34 sum = 0; 35 for( int i=0;i<n-1;i++ ){ 36 a[ i ] = k1*a[ i ]+k2*a[ i+1 ]; 37 sum += a[i]; 38 } 39 a[ n-1 ] = k1*a[ n-1 ]+k2*temp; 40 sum += a[ n-1 ]; 41 cnt++; 42 } 43 printf("Case #%d: ",ca++); 44 if( flag==1 ) printf("%I64d ",cnt); 45 else printf("inf "); 46 } 47 return 0; 48 }
后来推出公式
sigma(Ai)*(K1+K2)^t,t表示天数
1 /* 2 模拟 3 */ 4 #include<stdio.h> 5 #include<math.h> 6 const int maxn = 10005; 7 typedef __int64 int64; 8 int main(){ 9 int ca = 1; 10 int T; 11 scanf("%d",&T); 12 while( T-- ){ 13 printf("Case #%d: ",ca++); 14 int n,k1,k2; 15 int64 k; 16 scanf("%d%d%d%I64d",&n,&k1,&k2,&k); 17 int64 sum = 0; 18 int temp; 19 for( int i=0;i<n;i++ ){ 20 scanf("%d",&temp); 21 sum += temp; 22 } 23 if( sum>k ){ 24 printf("0 "); 25 continue; 26 } 27 if( abs(k1+k2)<=1 ){ 28 printf("inf "); 29 continue; 30 } 31 double ans1 = sum*1.0/100000.0; 32 double ans2 = (k)*1.0/100000.0; 33 int cnt = 0; 34 while( ans1<=ans2 ){ 35 cnt++; 36 ans1 = ans1*(k1+k2); 37 } 38 printf("%d ",cnt); 39 } 40 return 0; 41 }