Spare Tire(容斥)

A sequence of integer{an}{an}can be expressed as:
在这里插入图片描述
Now there are two integers nn and mm. I’m a pretty girl. I want to find all b1,b2,b3⋯bpb1,b2,b3⋯bpthat1⩽bi⩽n1⩽bi⩽n is relatively-prime with the integer mm. And then calculate:
在这里插入图片描述
But I have no time to solve this problem because I am going to date my boyfriend soon. So can you help me?

Input
Input contains multiple test cases ( about 15000 ). Each case contains two integers n and m. 1≤n,m≤1081≤n,m≤108 。

Output
For each test case, print the answer of my question(after mod 1,000,000,007).

Hint
In the all integers from 1 to 4, 1 and 3 is relatively-prime with the integer 4. So the answer is a_1+a_3=14

样例输入
4 4

样例输出
14

思路自
ACM-ICPC 2018 沈阳赛区网络预赛 G.Spare Tire (容斥)

代码实现

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=10005;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const ll inv6=166666668;
const ll inv2=500000004;

ll a[N];

ll cal(ll n,ll i)
{
    n=n/i;
    return (n%mod*(n+1)%mod*(2*n+1)%mod*inv6%mod*i%mod*i%mod+n%mod*(n+1)%mod*inv2%mod*i%mod)%mod;
}

int main()
{
    ll n,m;
    while(~scanf("%lld%lld",&n,&m))
    {
        int cnt=0;
        for(int i=2;i*i<=m;i++)
        {
            if(m%i==0)
            {
                a[cnt++]=i;
                while(m%i==0) m/=i;
            }
        }
        if(m!=1) a[cnt++]=m;
        ll ans=cal(n,1);
        ll ans2=0;
        for(int i=1;i<(1<<cnt);i++)
        {
            int flag=0;
            ll temp=1;
            for(int j=0;j<cnt;j++)
            {
                if(i&(1<<j))
                {
                    flag++;
                    temp=temp*a[j]%mod;
                }
            }
            temp=cal(n,temp);
            if(flag&1) ans2=(ans2%mod+temp%mod)%mod;
            else  ans2=(ans2%mod-temp%mod+mod)%mod;
        }
        printf("%lld\n",(ans%mod-ans2%mod+mod)%mod);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值