lightoj 1328 - A Gift from the Setter 【数学】
2016-04-27 13:56
1151 查看
题目链接:lightoj 1328 - A Gift from the Setter
1328 - A Gift from the Setter
PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB
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
求解函数值。
AC代码:
1328 - A Gift from the Setter
PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB
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
求解函数值。
AC代码:
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; typedef long long LL; const int MAXN = 1e5 +10; const int MOD = 1e6 + 7; LL a[MAXN]; LL sum[MAXN]; int main() { int t, kcase = 1; scanf("%d", &t); while(t--) { int K, C, n, v; scanf("%d%d%d%d", &K, &C, &n, &v); a[1] = v; for(int i = 2; i <= n; i++) { a[i] = (a[i-1] * K % MOD + C) % MOD; } sort(a+1, a+n+1); LL ans = 0; sum[0] = 0; for(int i = 1; i <= n; i++) { sum[i] = sum[i-1] + a[i]; } for(int i = 1; i <= n; i++) { ans += sum - sum[i] - 1LL * (n - i) * a[i]; } printf("Case %d: %lld\n", kcase++, ans); } return 0; }
相关文章推荐
- lightoj 1198 - Karate Competition 【贪心】
- Android中,Intent.setFlags();几个常用的属性
- WebService基本知识
- Spring MVC坑汇总+Stackoverflow巧解答
- StringBuilder类为何比string的简单拼接效率高
- 嵌入式开发板的学习步骤-基于iTOP-4412开发板
- block
- 高性能Web服务器Nginx的配置与部署研究(15)Upstream负载均衡模块
- 7.1趣味递归之打印杨辉三角
- Linux驱动开发之 二 (那些必须要了解的硬件知识 之 处理器篇)
- 安卓自定义超级提示框Dialog
- 图
- 通过eclipse生成doc
- 在线文档预览方案-office web apps
- Linux常用命令的简单总结
- Shader 坐标转换
- 【小松教你手游开发】【系统模块开发】图文混排 (在label中插入表情)
- final关键字用于String时的一个问题
- Memcached 集群的高可用(HA)架构
- bzoj 1251: 序列终结者(splay)