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. QT宏 Q_OBJECT,explicit, QHostAddress, quint, emit

    QT相關 一. 參考: 1.宏Q_OBJECT 二. explicit struct constrcution 三. QHostAddress Detailed Description: The QH ...

  2. (LightOJ 1149) Factors and Multiples

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1149 Description You will be given two sets o ...

  3. I Take It All Back: Using Windows Installer (MSI) Rollback Actions

    Original Link: http://blogs.flexerasoftware.com/installtalk/2011/10/i-take-it-all-back-using-windows ...

  4. struts2自定义拦截器与cookie整合实现用户免重复登入

    目的:测试开发时,为了减少用户登入这个繁琐的登入验证,就用struts2做了个简单的struts2拦截器,涉及到了与cookie整合,具体的看代码 结构(两部份)=struts2.xml+自定义拦截器 ...

  5. SQL Server 2012 读写分离设置

    SQL Server 2012 读写分离设置 - AlsoIn 时间 2014-07-21 17:38:00  博客园-所有随笔区 原文  http://www.cnblogs.com/also/p/ ...

  6. PHP 5.6.6 上运行 ecshop 2.7.3 不兼容问题整合

    在安装完php在自己的服务器上以后, 发现在静态网页上出现了很多 error. 在网上查找过后发现,大部分问题是因为 PHP发展到PHP5.5版本以后,有了很多细微的变化.而ECSHOP官方更新又太慢 ...

  7. Centos系统mysql 忘记root用户的密码

    Centos系统mysql 忘记root用户的密码: 第一步:(停掉正在运行的mysql) [root@maomao ~]# /etc/init.d/mysqld stop Stopping MySQ ...

  8. FireMonkey消息机制

    interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, FMX.Forms, FMX.Plat ...

  9. 整理sed实战修改多行配置技巧

    老男孩老师有关sed实战技巧分享,来自课堂教学内容实战1.在指定行前插入两行内容,分别为oldboy和oldgirl.提示:被修改的文件内容必须要大于等于2行 1 sed -i '2 ioldboy\ ...

  10. PythonCrawl自学日志(2)

    一.Scrapy环境的安装 1.配套组件的安装 由于开发环境是在VS2015Community中编码,默认下载的python3.5,系统是windows8.1,为此需要安装的组件有如下列表: 所有的组 ...