LightOJ - 1328 A Gift from the Setter (数学)
来源:互联网 发布:梦洁床垫怎么样知乎 编辑:程序博客网 时间:2024/05/19 15:22
SubmitStatus
Description
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:
longlongGetDiffSum(inta[],intn)
{
longlongsum=0;
inti,j;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
sum +=abs(a[i]-a[j]);// abs means absolute value
returnsum;
}
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 ofK, 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
2
1 1 2 1
10 10 10 5
Sample Output
Case 1: 1
Case 2: 7136758
Hint
Source
#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#define ll long long#define N 100010#define M 1000007using namespace std;ll a[N];ll sum[N];int main(){int t,T=1;int k,c,n,m;int i,j;scanf("%d",&t);while(t--){scanf("%d%d%d%d",&k,&c,&n,&m);a[0]=m;for(i=1;i<n;i++)a[i]=(k*a[i-1]+c)%M;sort(a,a+n);for(i=0;i<n;i++)sum[i]=sum[i-1]+a[i];ll num=0;for(i=0;i<n;i++){num+=sum[n-1]-sum[i-1]-a[i]*(n-i);}printf("Case %d: %lld\n",T++,num);}return 0;}
- LightOJ - 1328 A Gift from the Setter (数学)
- lightoj 1328 - A Gift from the Setter 【数学】
- LightOJ 1328 A Gift from the Setter
- Light OJ:1328 A Gift from the Setter(数学)
- lightoj 1328 - A Gift from the Setter 贪心
- Codeforces Round #342 (Div. 2)-A. Guest From the Past(数学)
- Codeforces 625A Guest From the Past 【基础数学】
- LightOJ 1349 - Aladdin and the Optimal Invitation(数学啊)
- LightOJ 1305 - Area of a Parallelogram(数学啊 )
- LightOJ - 1305 Area of a Parallelogram (数学几何)水
- lightoj 1305 Area of a Parallelogram (几何数学)
- lightoj(基础数学)
- ♥CodeForces 609B-B. The Best Gift【组合数学】
- Educational Codeforces Round 3 B.The Best Gift (数学)
- Aizu 1315 Gift from the Goddess of Programming 解题报告
- Aizu 1315 Gift from the Goddess of Programming
- lightoj 1095 - Arrange the Numbers 组合数学
- 【概率&数学期望】A Dangerous Maze LightOJ
- omnet++ 无法运行 环境变量 报错代码:-1073741511
- Android学习第四周-常用布局代码个人整合版
- 翻转链表
- fcitx候选词竖排
- HDU-ACM2070
- LightOJ - 1328 A Gift from the Setter (数学)
- jAVA接口回调
- http协议学习笔记(四)
- jQuery-Ajax
- 快稳炫:电商峰值系统架构三字诀
- Scala Worksheet
- PHP变量的定义与相应的数据类型
- java关于参数传递的思考
- NSNotification在UITextField的应用