CF——Codeforces Round #428 (Div. 2)D. Winter is here

[D. Winter is here](http://codeforces.com/contest/839/problem/D)

题解

1.分别统计a[n]中2-n的倍数的个数b[n],设c[n] = 2^(b[n]-1)-1;
2.GCD=i的组合个数 = c[i] - ∑c[j](j为i的倍数)(推导过程略)
3.ANS = ∑a[i](a[i]≠1) + ∑(GCD=i的组合个数 )(i≠1)

AC code
#include<bits/stdc++.h>
#define maxn 1000000 + 10
const int  MOD =  1000000000 + 7;
typedef long long LL;

using namespace std;
LL ans=0;
LL cnt[maxn];
LL a[maxn];
LL p[maxn];
void init()
{
	LL k = 1;
	p[0] = 1;
	for(int i = 1; i < maxn; i++)
	{	
		p[i] = (p[i-1]*2);
                p[i] = p[i] % MOD;
	}
//	cout << p[0] << " " << p[1] << " " << p[2] << endl;
}	
LL GCD(LL a,LL b)
{
	if(b == 0) return a;
	else return GCD(b,a%b);
}
int main()
{
	int n;
	cin >> n;
	init();
	memset(a,0,sizeof(a));
	memset(cnt,0,sizeof(cnt));
	for(int i = 0; i < n; i++)
	{
		int e;
		scanf("%I64d",&e);
		a[e]++;
		if(e>1) ans += e;
		ans %= MOD;
	}
	for(int i = 2; i <= 1000000; i++)
	{
		for(int j = i; j <= 1000000;j+=i)
		{
			cnt[i] += a[j];
		}
	//	if(cnt[i]) cout << cnt[i] << " " << p[cnt[i]-1] << endl;
		if(cnt[i])
			cnt[i] = (p[cnt[i]-1]-1)*cnt[i]%MOD;
	//	if(cnt[i]) cout << i << " " << cnt[i] << endl;
	}
	for(int i = 1000000; i >= 2; i--)
	{
		if(cnt[i] == 0) continue;
		for(int j = 2*i; j <= 1000000; j+=i)
		{
			cnt[i] -= cnt[j];
			cnt[i] = (cnt[i] + MOD)%MOD;
		}
		ans += cnt[i]*i;
		ans %= MOD;
	}
	cout << ans%MOD << endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值