Codeforces Round #428 (Div. 2) D

来源:互联网 发布:做网站必备软件 编辑:程序博客网 时间:2020/10/20 16:30

答案就是所有能得到gcd的子序列的总长度
所以考虑把这个和求出来
假如有x个i的倍数
那所求和就为1*C(x,1)+ 2 * C(x,2) + …… + x * C(x,x)

从大往小枚举gcd
再减掉每个gcd的倍数的答案

#include <iostream>#include <algorithm>#include <sstream>#include <string>#include <queue>#include <cstdio>#include <map>#include <set>#include <utility>#include <stack>#include <cstring>#include <cmath>#include <vector>#include <ctime>using namespace std;#define pb push_back#define sd(n) scanf("%d",&n)#define sdd(n,m) scanf("%d%d",&n,&m)#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)#define sld(n) scanf("%lld",&n)#define sldd(n,m) scanf("%lld%lld",&n,&m)#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)#define sf(n) scanf("%lf",&n)#define sff(n,m) scanf("%lf%lf",&n,&m)#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)#define ss(str) scanf("%s",str)#define ans() printf("%d",ans)#define ansn() printf("%d\n",ans)#define anss() printf("%d ",ans)#define lans() printf("%lld",ans)#define lanss() printf("%lld ",ans)#define lansn() printf("%lld\n",ans)#define r0(i,n) for(int i=0;i<(n);++i)#define r1(i,e) for(int i=1;i<=e;++i)#define rn(i,e) for(int i=e;i>=1;--i)#define rsz(i,v) for(int i=0;i<(int)v.size();++i)#define szz(x) ((int)x.size())#define mst(abc,bca) memset(abc,bca,sizeof abc)#define lowbit(a) (a&(-a))#define all(a) a.begin(),a.end()#define pii pair<int,int>#define pli pair<ll,int>#define mp make_pair#define lrt rt<<1#define rrt rt<<1|1#define X first#define Y second#define PI (acos(-1.0))typedef long long ll;typedef unsigned long long ull;const int mod = 1000000000+7;const double eps=1e-9;const int inf=0x3f3f3f3f;const ll infl = 10000000000000000;const int maxn=  200000+10;const int maxm = 1000000+10;int in(int &ret){    char c;    int sgn ;    if(c=getchar(),c==EOF)return -1;    while(c!='-'&&(c<'0'||c>'9'))c=getchar();    sgn = (c=='-')?-1:1;    ret = (c=='-')?0:(c-'0');    while(c=getchar(),c>='0'&&c<='9')ret = ret*10+(c-'0');    ret *=sgn;    return 1;}int p[maxn];int cnt[maxm];int dp[maxm];int main(){#ifdef LOCAL    freopen("input.txt","r",stdin);//    freopen("output.txt","w",stdout);#endif // LOCAL//    freopen("input.txt","r",stdin);//    freopen("output.txt","w",stdout);    p[0]=1;    int n;sd(n);    r1(i,n)p[i] = p[i-1]*2%mod;    int mx = 0;    while(n--)    {        int x;sd(x);        cnt[x]++;mx = max(mx,x);    }    ll ans =0 ;    for(int i=mx;i>=2;--i)    {        int x = 0 , tmp = 0;        for(int j=i;j<=mx;j+=i)        {            x+=cnt[j];            tmp = (tmp - dp[j] +mod)%mod;        }        dp[i] = ((1LL*x*p[x-1]%mod)+tmp)%mod;        ans= (ans+1LL*dp[i]*i)%mod;    }    lansn();    return 0;}
原创粉丝点击