您的位置:首页 > 其它

codeforces 810C Do you want a date? 数学 快速幂取模

2017-05-26 11:52 393 查看
题意:从一个含n个元素 的整数集中,取至少含有两个元素的子集,求所有子集a,

,f(a)之和,输出结果为对1e9取模

思路:http://blog.csdn.net/dragon60066/article/details/72599167

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 3* (1e5+10);
const int mod = 1e9+7;
int x[maxn];
ll presum[maxn];
ll ksm(ll a,ll b)
{
ll base = a;ll ans=1;
while(b)
{
if(b&1)ans=(base*ans)%mod;
base*=base;
base%=mod;
b=b>>1;
}
return ans;
}
int main()
{
int n;
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
presum[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&x[i]);
}
sort(x+1,x+n+1);

presum[0]=x
-x[1];
for(int i=1;i<=n-2;i++)
presum[i]=presum[i-1]+x[n-i]-x[i+1];

ll ans=0;
for(int i=0;i<=n-2;i++){//1 3 4
ans = (ans+ksm(2,i)*(presum[i]%mod))%mod;
}
ans%=mod;
printf("%I64d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: