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:
long long GetDiffSum( int a[], int n )
{
long long sum = ;
int i, j;
for( i = ; i < n; i++ )
for( j = i + ; 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 KC 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

 
 
 
 
 
 
先根据递推式求出每一项,再nlogn求出两两只差绝对值的和。

nlogn求差的绝对值之和:
假设有5个数:a[1],a[2],a[3],a[4],a[5],则:
先排序、然后每次把每个数后面的数与其作差、
比如考虑a[1]时、则算出a[2]-a[1],a[3]-[1],a[4]-a[1],a[5]-a[1],
其中:a[3]-a[1]=a[3]-a[2]+a[2]-a[1]
a[4]-a[1]=a[4]-a[3]+a[3]-a[2]+a[2]-a[1]
a[5]-a[1]=a[5]-a[4]+a[4]-a[3]+a[3]-a[2]+a[2]-a[1]
这样显然可以直接得出:a[2]-a[1]算了4次,a[3]-a[2]算了3次,a[4]-a[3]算了2次,a[5]-a[4]算了1次。
然后再依次考虑a[2]时,                        a[3]-a[2]算了3次,a[4]-a[3]算了2次,a[5]-a[4]算了1次。
然后再依次考虑a[3]时,                                                 a[4]-a[3]算了2次,a[5]-a[4]算了1次。
然后再依次考虑a[4]时,                                                                          a[5]-a[4]算了1次。
统计一下:a[2]-a[1]算了1*4次,a[3]-a[2]算了2*3次,a[4]-a[3]算了3*2次,a[5]-a[4]算了4*1次。

综上、O(nlogn)排序,O(n)统计

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
#define INF 0x7fffffff
#define MOD 1000007
#define N 100010 ll sum;
ll a[N];
ll b[N];
ll k,c,n; int main()
{
ll T,i,j,iCase=;
scanf("%lld",&T);
while(T--)
{
scanf("%lld%lld%lld%lld",&k,&c,&n,&a[]);
for(i=;i<n;i++)
{
a[i]=(k*a[i-]+c)%MOD;
}
sort(a,a+n);
sum=;
for(i=;i<n;i++)
{
sum+=(n-i)*i*(a[i]-a[i-]);
}
printf("Case %lld: %lld\n",iCase++,sum);
}
return ;
}

[light oj 1328] A Gift from the Setter的更多相关文章

  1. Light OJ 1114 Easily Readable 字典树

    题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...

  2. Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

    题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...

  3. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

  4. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  5. light oj 1007 Mathematically Hard (欧拉函数)

    题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...

  6. Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖

    题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...

  7. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

  8. Jan's light oj 01--二分搜索篇

    碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...

  9. Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...

随机推荐

  1. CodeForces 538B

    Description A number is called quasibinary if its decimal representation contains only digits 0 or 1 ...

  2. GZIP 头解析

    在用HttpWebRequest对象时,一般我们都没有开启gzip压缩,如果服务端返回的数据比较大,这是我们需要开启gzip压缩,怎么开启呢? 1.给HttpWebRequest对象,添加如下Head ...

  3. 小shell函数

     whoport() {  port=$1  echo "------ who occupied port: $port ----------"  info=$(sudo lsof ...

  4. Mac OS X平台上Java环境的配置

    最近换了工作,以前是做c/c++的,但是现在公司的主打产品是使用Java开发,为了以后维护代码,现在开始抽空学习一下Java相关的内容. 在学习之前,首先需要搭建各种平台的开发环境,而我选用的操作系统 ...

  5. Oracle 自连接 / 外连接 / 子查询

    --连接查询的三种格式 select ename, deptno,dname from emp natural join dept; select ename, deptno,dname from e ...

  6. ACE_linux:读写锁

    1.涉及类 ACE_RW_Thread_Mutex //ACE读写锁ACE_Read_Guard //ACE加读锁ACE_Write_Guard //ACE加写锁ACE_Thread_Manager ...

  7. css实现div块半透明

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 推荐:PHPCMS v9 安全防范教程!

    一.目录权限设置很重要:可以有效防范黑客上传木马文件. 如果通过 chmod 644 * -R 的话,php文件就没有权限访问了. 如果通过chmod 755 * -R 的话,php文件的权限就高了. ...

  9. 基于BOOST 实现并发服务器框架

    一:设计思路 本服务器框架使用 UDP 传输协议,程序柱线程等待客户端数据,并将数组存取队列缓冲区.另外可开启多个工作线程,工作线程可以依据具体项目实现不同的功能 ,例如可以将队列缓冲区中的数据逐个取 ...

  10. TCP UDP 协议的选择

    行业应用中TCP/IP传输协议和UDP协议的选择! 中国移动.中国联通推行的GPRS网络.CDMA网络已覆盖大量的区域,通过无线网络实现数据传输成为可 能.无线Modem采用GPRS.CDMA模块通过 ...