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);
}
}
,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);
}
}
相关文章推荐
- Codeforces 810C Do you want a date?(数学,前缀和)
- CodeForces 810C Do you want a date? 【数学】【排序】
- Codeforces 810C Do you want a date 枚举
- Codeforces 810C Do you want a date? 题解
- Codeforces 810C Do you want a date?【思维】
- Codeforces 809A Do you want a date?
- UVa 10900 So you want to be a 2n-aire? (概率DP,数学)
- CF - 810C. Do you want a date? - 数学+排序+前缀和思维+快速幂取模
- 【组合 数学】codeforces C. Do you want a date?
- CodeForces 810C - Do you want a date?(数学+排序)
- Codeforces Round #415 (Div. 1) A Do you want a date?
- UVA 10900 So you want to be a 2n-aire? 2元富翁 (数学期望,贪心)
- 87.You want to display the date for the first Mon day of the next month and
- codeforces809A Do you want a date?
- Codeforces 151C Quantity of Strings(快速幂+数学分情况)
- Codeforces - 551D. GukiZ and Binary Operations - 数学+矩阵快速幂
- CodeForces-696C Please(数学题,快速幂取模,乘法逆元)
- CodeForces 359C Prime Number 数学+快速幂
- Codeforces 665D Simple Subset [简单数学]
- Codeforces 622F 「数学数论」「数学规律」