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;
}