【ACM-ICPC 2018 沈阳赛区网络预赛 G】Spare Tire

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

让你求出1..n中和m互质的位置i. 让你输出∑ai 这个ai可以oeis一波。 发现是ai = i*(i+1) 1..n中和m互质的数字的个数之前有做过一题。 然后发现是逆着做的。 删掉不互质的。剩下的就是互质的了。 是用容斥原理搞的。 其中有ans+=n/temp和ass-=n/temp 表示的是加上temp,2*temp,3*temp...t*temp这些数字 以及减去temp,2*temp,3*temp...t*temp这些数字 放在这一题的话其实就是 ans+=a[temp]+a[2*temp]...+a[t*temp] 然后发现是等差的下标。那么就去推推公式吧? 然后发现真的能推出来 在get_ans2里。自己看吧。 那么ans就是下标和m不互质的ai加起来的和 然后ans=$∑_1^na_i$-ans.

【代码】

#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define lson l,mid,rt<<1
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x)
#define rson mid+1,r,rt<<1|1
using namespace std;

const int N = 10000;
const LL MOD = 1e9+7;

LL n,m,pri[N+10];
LL sixni;
int num;

LL Pow(LL x,LL y){ //求x^y
    LL a = 1;
    while (y){
        if (y&1) a = (a*x)%MOD;
        x=(x*x)%MOD;
        y>>=1;
    }
    return a;
}

LL get_ans2(LL t,LL x){
    LL temp1 = t*(t+1)%MOD*(2*t+1)%MOD;
    temp1 = temp1*sixni%MOD*x%MOD*x%MOD;
    temp1 = temp1 + (1+t)*t/2%MOD*x%MOD;
    return temp1;
}

LL get_ans(LL x)
{
    LL ans = 0;
    rep1(i,1,(1<<num)-1){
        LL y = 1,f = 0;
        rep1(j,1,num)
            if (i & (1<<(j-1))){
                y = y*pri[j];
                f++;
            }
        if (f&1)
            ans += get_ans2(x/y,y);
        else
            ans -= get_ans2(x/y,y);
    }

    ans = 2*(n+2)*(n+1)%MOD*n%MOD*sixni%MOD-ans;
    ans = ans%MOD;
    ans=(ans+MOD)%MOD;
    return ans;
}

void _init(){
    num = 0;
    for (LL i = 2;i*i<=m;i++)
        if ((m%i)==0){
            pri[++num] = i;
            while ((m%i)==0) m/=i;
        }
    if (m > 1) pri[++num] = m;
}

int main()
{
    sixni=Pow(6,MOD-2);
    while (~scanf("%lld%lld",&n,&m)){
        _init();
        printf("%lld
",get_ans(n));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/9626655.html