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. tomcat错误信息解决方案【严重:StandardServer.await: create[8005]

    1.独立运行的tomcat.exe没有关闭,关闭tomcat图标并结束掉tomcat进程.(我是这个原因,在开始菜单里找到tomcat,然后stop它) 2.安装了其他的软件占用了8080端口,tom ...

  2. hdu 5009 Paint Pearls

    首先把具有相同颜色的点缩成一个点,即数据离散化. 然后使用dp[i]表示涂满前i个点的最小代价.对于第i+1个点,有两种情况: 1)自己单独涂,即dp[i+1] = dp[i] + 1 2)从第k个节 ...

  3. JQuery(三) Ajax相关

    JQuery大大简化了Ajax通用操作,开发者只需要指定请求URL,回调函数即可. 三个主要方法: $().param(obj):将obj参数(对象或数组)转化成查询字符串. {name:" ...

  4. Linux学习笔记2

    1.系统引导配置文件  # vi /boot/grub/grub.conf   default=0   timeout=5   splashimage=(hd0,0)/grub/splash.xpm. ...

  5. python 列表推导的注意点

    Code a = [1,2,2,3] b = [for item in a if item not in b] c = [] for item in a: if item not in c: c.ap ...

  6. configure: error: zlib library and headers are required

    configure: error: zlib library and headers are required (1)直接看是zlib没安装导致的,yum list |grep zlib* 看到的是全 ...

  7. WPF一个简单的垂直菜单样式的实现

    以前制作类似于垂直菜单功能的控件我都是Listbox和一个Popup实现的,今天尝试着用Menu做了一个简单垂直菜单,就当是做了个小练习写了这篇随笔~: 有什么不对的地方希望大家指正,分享和记录也是一 ...

  8. WebService 学习总结

    一.概念 Web Web应用程序 Web服务( Web Serivce), SOAP, WSDL, UDDI .Net 框架 ASP.net IIS C#, 代理(委托) 二.实践 1.创建WebSe ...

  9. 检测浏览器对HTML5和CSS3支持情况的利器——Modernizr

    Modernizr是什么? Modernizr 是一个用来检测浏览器功能支持情况的 JavaScript 库. 目前,通过检验浏览器对一系列测试的处理情况,Modernizr 可以检测18项 CSS3 ...

  10. <三> SQL杂七杂八

    批量插入测试数据 use Testdeclare @count INTset @count = 0while(@count < 10)begin waitfor delay '000:00:10 ...