Codeforces839D Winter is here (数论:容斥原理)
2017-08-18 11:25
197 查看
D. Winter is here
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the
world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers.
He has created a method to know how strong his army is. Let the i-th soldier’s strength be ai.
For some k he calls i1, i2, ..., ik a
clan ifi1 < i2 < i3 < ... < ik and gcd(ai1, ai2, ..., aik) > 1 .
He calls the strength of that clan k·gcd(ai1, ai2, ..., aik).
Then he defines the strength of his army by the sum of strengths of all possible clans.
Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109 + 7).
Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it.
Input
The first line contains integer n (1 ≤ n ≤ 200000) —
the size of the army.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000000) —
denoting the strengths of his soldiers.
Output
Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7).
Examples
input
output
input
output
Note
In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12
题意:
给出一些数,求其中GCD不为1的数的序列长度乘以对应的GCD之和;emmmm。。主要还是看例子看懂的。
思路:
首先要知道有n个数的话,那么这n个数组合长度不为零的组合一共有n*(2^(n-1))种,知道这个规律后,接下来我们就假设F(x)就是这个公式的返回值。还需要考虑一种情况,例如如果GCD为4的时候,但这种情况下在2中会多记录4中的情况。这时候就遵循容斥原理,此时2中的数量就等于F(2) - F(4) - F(8) ....-F(2*n),即某个数的情况数和就等于F(x)减去x倍数的F。有了这条规律就好做了。
代码:
#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1e6 + 9;
const int MOD = 1e9+7;
long long vec[MAX_N];
long long sum[MAX_N];
long long my_pow(long long x,int k)
{
long long res = 1;
while(k>0)//发现原来while(k)的用法只会在k=0的时候才break,在k<0的时候并不会break。!
{
k--;
res *= x;
res %= MOD;//emmm。这个MOD没有加上卡了我好久
}
return res;
}
int main()
{
int N,M,T;
while(~scanf("%d",&N))
{
memset(vec,0,sizeof(vec));
memset(sum,0,sizeof(sum));
int maxn = -1;
for(int i=0; i<N; i++)
{
int temp;
scanf("%d",&temp);
//本来这里我写的是循环把每个数的因数都求出来,后面借鉴了别人的代码后
//发现其实这一步操作可以和求数量的操作一起进行
vec[temp]++;
maxn = max(maxn, temp);
}
long long ans = 0;
for(int i=maxn; i>=2; i--)
{
//if(vec[i])
//这里千万不能写这条,因为比方说现在有GCD为4和8的集合那么vec[2]
//就为0,但是其实GCD为2的集合数量并不为0
long long tot = 0;
for(int j=i; j<=maxn; j+=i)
{
sum[i] -= sum[j];
tot += vec[j];
}
sum[i] += (tot*my_pow(2,tot-1)+ MOD) %MOD; //要加上MOD,因为sum减去后有可能为负值
ans += (i*sum[i])%MOD;
ans = (ans+MOD)%MOD;
}
cout<<ans<<endl;
}
return 0;
}
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the
world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers.
He has created a method to know how strong his army is. Let the i-th soldier’s strength be ai.
For some k he calls i1, i2, ..., ik a
clan ifi1 < i2 < i3 < ... < ik and gcd(ai1, ai2, ..., aik) > 1 .
He calls the strength of that clan k·gcd(ai1, ai2, ..., aik).
Then he defines the strength of his army by the sum of strengths of all possible clans.
Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109 + 7).
Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it.
Input
The first line contains integer n (1 ≤ n ≤ 200000) —
the size of the army.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000000) —
denoting the strengths of his soldiers.
Output
Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7).
Examples
input
3 3 3 1
output
12
input
4 2 3 4 6
output
39
Note
In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12
题意:
给出一些数,求其中GCD不为1的数的序列长度乘以对应的GCD之和;emmmm。。主要还是看例子看懂的。
思路:
首先要知道有n个数的话,那么这n个数组合长度不为零的组合一共有n*(2^(n-1))种,知道这个规律后,接下来我们就假设F(x)就是这个公式的返回值。还需要考虑一种情况,例如如果GCD为4的时候,但这种情况下在2中会多记录4中的情况。这时候就遵循容斥原理,此时2中的数量就等于F(2) - F(4) - F(8) ....-F(2*n),即某个数的情况数和就等于F(x)减去x倍数的F。有了这条规律就好做了。
代码:
#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1e6 + 9;
const int MOD = 1e9+7;
long long vec[MAX_N];
long long sum[MAX_N];
long long my_pow(long long x,int k)
{
long long res = 1;
while(k>0)//发现原来while(k)的用法只会在k=0的时候才break,在k<0的时候并不会break。!
{
k--;
res *= x;
res %= MOD;//emmm。这个MOD没有加上卡了我好久
}
return res;
}
int main()
{
int N,M,T;
while(~scanf("%d",&N))
{
memset(vec,0,sizeof(vec));
memset(sum,0,sizeof(sum));
int maxn = -1;
for(int i=0; i<N; i++)
{
int temp;
scanf("%d",&temp);
//本来这里我写的是循环把每个数的因数都求出来,后面借鉴了别人的代码后
//发现其实这一步操作可以和求数量的操作一起进行
vec[temp]++;
maxn = max(maxn, temp);
}
long long ans = 0;
for(int i=maxn; i>=2; i--)
{
//if(vec[i])
//这里千万不能写这条,因为比方说现在有GCD为4和8的集合那么vec[2]
//就为0,但是其实GCD为2的集合数量并不为0
long long tot = 0;
for(int j=i; j<=maxn; j+=i)
{
sum[i] -= sum[j];
tot += vec[j];
}
sum[i] += (tot*my_pow(2,tot-1)+ MOD) %MOD; //要加上MOD,因为sum减去后有可能为负值
ans += (i*sum[i])%MOD;
ans = (ans+MOD)%MOD;
}
cout<<ans<<endl;
}
return 0;
}
相关文章推荐
- HDU 1695 GCD (数论-整数和素数,欧拉函数,组合数学-容斥原理)
- 【数论】【容斥原理】八
- hdu 2204 Eddy's爱好 (数论,容斥原理)
- 数论训练 {限制} [扩展gcd][组合数][容斥原理]
- HDU 2841 Visible Trees (数论,容斥原理)
- hdu4059 2011年大连区域赛 数论(四次方和 容斥原理 取模)
- hdu——3501(数论之容斥原理)
- hdu 1659 综合数论+ 筛选欧拉函数 +质因子 +容斥原理
- 容斥原理 数论
- hdu 1695 综合数论 欧拉函数 分解质因子 容斥原理 打印素数表 帅呆了的一个题目 详解
- 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7
- 数论+容斥原理 --hdu 上的几题
- NOIP模拟赛 数论专题 扩展欧几里得 + 组合数 + 容斥原理
- URAL 1907. Coffee and Buns(数论推导+容斥原理)
- HDU 2204 Eddy's爱好(数论,容斥原理)
- ZOJ 3233 Lucky Number(数论,容斥原理)
- HDU 1695 GCD 【数论,容斥原理】
- 校内互测 光 (数论+容斥原理)
- HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)
- HDU 2841 Visible Trees(容斥原理,数论)