Light OJ:1328 A Gift from the Setter(数学)
2016-04-28 18:22
363 查看
1328 - A Gift from the Setter
Problem setting is somewhat of a cruel task of throwing something at the contestants and having them scratch their head to derive a solution. In this problem, the setter is a bit kind and has decided to
gift the contestants an algorithm which they should code and submit. The C/C++ equivalent code of the algorithm is given below:
The values of array a[] are generated by the following recurrence relation:
a[i] = (K * a[i-1] + C) % 1000007 for i > 0
where K, C and a[0] are predefined values. In this problem, given the values of K, C,
n and a[0], you have find the result of the function
"long long GetDiffSum( int a[], int n )"
But the setter soon realizes that the straight forward implementation of the code is not efficient enough and may return the famous "TLE" and that's why he asks you to optimize the code.
Each case contains four integers K, C, n and a[0]. You can assume that (1 ≤ K, C, a[0] ≤ 104) and (2
≤ n ≤ 105).
题意:计算出上面那个代码的返回值。
思路:先根据递推公式,将a[]的数组填上,然后找规律。
规律:假设a1、a2、a3、a4、a5这五个数吧,先将其从大到小排列。根据那个代码的意思,对于a1计算时:a1-a2+a1-a3+a1-a4+a1-a5=4*a1-a2-a3-a4-a5;对于a2计算时:a2-a3+a2-a4+a2-a5=3*a2-a3-a4-a5;对于a3计算时:a3-a4+a3-a5=2*a3-a4-a5;对于a4计算时:a4-a5。
将上面的得到的相加得sum值:可得规律为4*a1+2*a2+0*a3-2*a4-4*a5。推广下,另flag=n-1,则sum=(flag)*a1+(flag-2)*a2+(flag-2-2)*a3+...+(flag-2-2-...-2)an(循环n次)。
注意用long long,要不然wa。。。
Problem setting is somewhat of a cruel task of throwing something at the contestants and having them scratch their head to derive a solution. In this problem, the setter is a bit kind and has decided to
gift the contestants an algorithm which they should code and submit. The C/C++ equivalent code of the algorithm is given below:
long long GetDiffSum( int a[], int n ) { long long sum = 0; int i, j; for( i = 0; i < n; i++ ) for( j = i + 1; j < n; j++ ) sum += abs( a[i] - a[j] ); // abs means absolute value return sum; }
The values of array a[] are generated by the following recurrence relation:
a[i] = (K * a[i-1] + C) % 1000007 for i > 0
where K, C and a[0] are predefined values. In this problem, given the values of K, C,
n and a[0], you have find the result of the function
"long long GetDiffSum( int a[], int n )"
But the setter soon realizes that the straight forward implementation of the code is not efficient enough and may return the famous "TLE" and that's why he asks you to optimize the code.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.Each case contains four integers K, C, n and a[0]. You can assume that (1 ≤ K, C, a[0] ≤ 104) and (2
≤ n ≤ 105).
Output
For each case, print the case number and the value returned by the function as stated above.Sample Input | Output for Sample Input |
2 1 1 2 1 10 10 10 5 | Case 1: 1 Case 2: 7136758 |
题意:计算出上面那个代码的返回值。
思路:先根据递推公式,将a[]的数组填上,然后找规律。
规律:假设a1、a2、a3、a4、a5这五个数吧,先将其从大到小排列。根据那个代码的意思,对于a1计算时:a1-a2+a1-a3+a1-a4+a1-a5=4*a1-a2-a3-a4-a5;对于a2计算时:a2-a3+a2-a4+a2-a5=3*a2-a3-a4-a5;对于a3计算时:a3-a4+a3-a5=2*a3-a4-a5;对于a4计算时:a4-a5。
将上面的得到的相加得sum值:可得规律为4*a1+2*a2+0*a3-2*a4-4*a5。推广下,另flag=n-1,则sum=(flag)*a1+(flag-2)*a2+(flag-2-2)*a3+...+(flag-2-2-...-2)an(循环n次)。
注意用long long,要不然wa。。。
#include <stdio.h> #include <algorithm> using namespace std; long long a[100010]; long long k,c,n; void dabiao() { for(long long i=2;i<=n;i++) { a[i]=(k*a[i-1]+c)%1000007; } } long long cmp(long long x,long long y) { return x>y; } int main() { long long t; scanf("%lld",&t); long long cnt=1; while(t--) { scanf("%lld%lld%lld%lld",&k,&c,&n,&a[1]); dabiao(); sort(a+1,a+1+n,cmp); long long flag=n-1; long long sum=0; for(long long i=1;i<=n;i++) { sum=sum+flag*a[i]; flag=flag-2; } printf("Case %lld: %lld\n",cnt,sum); cnt++; } return 0; }