[cf839d]Winter is here容斥原理

题意:给定一个数列ai,若子序列长度为k,最大公约数为gcd,定义子序列的权值为k⋅gcd⋅[gcd>1]。求所有子序列的权值和。 
答案对10^9+7取模。

解题关键:容斥原理求序列中各gcd的个数,亦可用莫比乌斯函数。

逆序求的话,前面直接减后面的个数,在后面一项就相当于相加了,如此往复。

关于知道所有gcd为n的个数之后答案的求法:

法一:

$\begin{array}{l}
1C_n^1 + 2C_n^2 + ... + nC_n^n\\
= n(C_{n - 1}^1 + C_{n - 1}^2 + ... + C_{n - 1}^{n - 1})\\
= n{2^{n - 1}}
\end{array}$

法二:

$\begin{array}{l}
[{(x + 1)^n}]‘ = n{(x + 1)^{n - 1}}\\
{(x + 1)^n} = \sum\limits_{i = 1}^n {C_n^i{x^i}} \\
n{(x + 1)^{n - 1}} = \sum\limits_{i = 1}^n {C_n^ii{x^{i - 1}}}
\end{array}$

法三:逆序相加

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int mod=1e9+7;
 5 #define inf 0x3f3f3f3f
 6 ll c[1000004],pw[1000007],sum[1000007];
 7 int main(){
 8     ll n,x,mx=-inf;
 9     cin>>n;
10     pw[0]=1;
11     for(int i=1;i<=n+3;i++) pw[i]=pw[i-1]*2%mod;
12     for(int i=0;i<n;i++) cin>>x,c[x]++,mx=max(mx,x);//hash一下
13     ll ct;
14     ll ans=0;
15     for(int i=mx;i>1;i--){
16         ct=0;
17         for(int j=i;j<=mx;j+=i){
18             ct=(ct+c[j])%mod;
19             sum[i]-=sum[j];
20         }
21         sum[i]=(sum[i]+ct*pw[ct-1]%mod+mod)%mod;
22         ans=(ans+sum[i]*i%mod+mod)%mod;
23     }
24     cout<<ans<<"\n";
25     return 0;
26 }
时间: 2024-12-26 08:26:21

[cf839d]Winter is here容斥原理的相关文章

Codeforces 839D Winter is here - 暴力 - 容斥原理

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 m

【容斥原理】Codeforces Round #428 (Div. 2) D. Winter is here

给你一个序列,让你对于所有gcd不为1的子序列,计算它们的gcd*其元素个数之和. 设sum(i)为i的倍数的数的个数,可以通过容斥算出来. 具体看这个吧:http://blog.csdn.net/jaihk662/article/details/77161436. 注意1*C(n,1)+2*C(n,2)+...+n*C(n,n)=n*2^(n-1). #include<cstdio> using namespace std; typedef long long ll; #define MOD

Codeforces 839D Winter is here(容斥原理)

[题目链接] http://codeforces.com/contest/839/problem/D [题目大意] 给出一些数,求取出一些数,当他们的GCD大于0时,将数量乘GCD累加到答案上, 求累加和. [题解] 我们枚举GCD,统计为其倍数的数字数量,先假定其能组成的集合数为贡献, 但是我们发现在统计的过程中有多余统计的部分,比如4和8是2的倍数, 然而它们的GCD等于4,所以我们对于每个集合数的贡献要减去所有其倍数的集合数的贡献, 倒着容斥即可. [代码] #include <cstdi

Codeforces Round #428 (Div. 2) D. Winter is here[数论II][容斥原理]

传送门:http://codeforces.com/contest/839/problem/D Examples input 33 3 1 output 12 input 42 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 题解:当有n个数为x的倍数时 gcd为x对答案的贡献为$1*C_n^1+2*C_n^2+..

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

BZOJ3198 SDOI2013 spring HASH+容斥原理

题意:给定6个长度为n的数列,求有多少个数对(i,j)((i,j)≡(j,i))使得i和j位置恰好有K个数相同,其中0≤K≤6 题解: 设fi=至少有K个数相同的位置对的数量,用2^6枚举每一种可能,根据容斥原理,答案就是\[\sum\limits_{i = K}^N {{f_i}C_i^K{{\left( { - 1} \right)}^{i - K}}} \] 至于多乘一个组合数,是因为当前枚举到有x个数相同,一个位置对有i个相同的数,那么累计的时候就会算成$C_x^i$,因此实际上这个位置

hdu 4790 Just Random 容斥原理+数学

Just Random Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1723    Accepted Submission(s): 483 Problem Description Coach Pang and Uncle Yang both love numbers. Every morning they play a game w

[BZOJ 1042] 硬币购物 容斥原理

题意 有四种货币, 它们的价值分别是 c[0], c[1], c[2], c[3] . n 次询问, 每次给定 d[0], d[1], d[2], d[3], s, 问凑出 s , 且第 i 种货币不超过 c[i] 个的方案数. c[i], d[i], s <= 100000 , n <= 1000 . 分析 设第 i 种货币取了 x[i] 个. 问题转化为求不定方程 c[0]x[0] + c[1]x[1] + c[2]x[2] + c[3]x[3] = s 的非负整数解的个数. 且满足 4

HDU - 4135 Co-prime(容斥原理)

Question 参考 题意找出[a,b]中与n互质的数的个数分析通常我们求1-n中与n互质的数的个数都是用欧拉函数.但如果n比较大或者是求1-m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理.先对n分解质因数,分别记录每个质因数, 那么所求区间内与某个质因数不互质的个数就是 m/r(i),假设r(i)是r的某个质因子 假设只有三个质因子, 总的不互质的个数应该为p1+p2+p3-p1*p2-p1*p3-p2*p3+p1*p2*p3. pi代表m/r(i),即与某个质因子不互质的