codechef(MAY15)第二题Set Difference
来源:互联网 发布:mysql 查询某一年数据 编辑:程序博客网 时间:2024/06/05 18:26
http://www.codechef.com/MAY15/第二题
刚开始找到的规律是对NUM数组进行两个FOR循环,答案就是(num[j]-num[i])*make_pow(2,j-i-1,mod),但是两个循环一定是超的。
然后就联想到吧这个规律×进去计算看看,结果发现,是有规律的。可以举个例子数列1,2,3,4来计算来找规律。
另外有一个坑就是代码里面不能写成ans+=......应为会超过数据的范围
#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#include <string>#include <set>#include <functional>#include <numeric>#include <sstream>#include <stack>#include <map>#include <queue>using namespace std;long long make_pow(long long x,long long n,long long mod){long long res =1;while(n>0){if(n&1)res = res*x%mod;x = x*x%mod;n>>=1;}return res;}long long num[100010];int main(){long long T;cin >> T;while(T--){long long n;cin >> n;for(int i=0;i<n;i++){cin >> num[i];}sort(num,num+n);long long ans = 0;for(int i=0;i<n;i++){ans=(ans+(num[i])*(make_pow(2,i,1000000007))%1000000007)%1000000007;ans=(ans-(num[i])*(make_pow(2,n-i-1,1000000007))%1000000007+1000000007)%1000000007;}cout<<ans<<endl;}return 0;}
0 0
- codechef(MAY15)第二题Set Difference
- 【codechef】Set Difference (灵活题,超时)
- codechef(MAY15)第三题Chef and Prime Divisors
- codechef-Set Difference
- Codechef Maximum Weight Difference题解
- Set Difference(所有子集的最值差)
- CodeChef:L-R queries(线段树 & set)
- 【codechef】Gcd Queries(灵活题,分类)
- 【codechef】 Zeroes(大数灵活题)
- 【codechef】Arush Challenge(灵活题)
- 【codechef】Strings Classes(枚举灵活题)
- 【codechef】Magical Transformation(dp,技巧题)
- 【codechef】The Warehouse(灵活题)
- CodeChef补题计划
- CodeChef FNCS(分块)
- CodeChef:Devasena(数论)
- CodeChef
- CodeChef
- HDU3584 Cube【树状数组】【三维】
- Openstack Nova Failed to allocate network(s)
- LinearLayout增加边框
- 解压vmlinuz和解压initrd(initramfs)
- Android Tween and Property Animation的本质区别
- codechef(MAY15)第二题Set Difference
- MySQL与Oracle的分页查询语句
- Swift学习:8.字典
- WampServer安装遇到的问题解决
- 国内开源的框架(学习用)以及语言对应的图书
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)
- c++读取obj文件的数据 其中f中只取面片定点数据
- 为什么学到现在任然对计算机浑然不知???门外汉??? 对其运行机制不能理解?
- 致大家