要使跳蚤可以跳到起始点左边一个单位,要保证卡片上N+1个数字的 gcd=1 ,否则的话,跳蚤跳到距离起始点左边或者右边的距离一定不能躲开他们之间的公因子。这就好求了,就是求有多少种方案使得这N+1个数字的gcd为1,这个并不好求,但是我们可以求有多少种方案使得N+1个数字的 gcd!=1 。因为M是最大的,所以这个gcd一定是M的因子,我们可以把M素因子分解,然后枚举M的素因子进行容斥。假设 gcd=p ,则有 M/p 个数可以选择,前N位数的方案数就是 (M/p)n ,然后对M的素因子容斥,最后用 Mn 减掉就是结果了
#include <iostream>
using namespace std;
typedef __int64 LL;
int p[50];
int plen;
int n,m;
LL pow(LL a, int b)
{
LL ret = 1;
while(b)
{
if(b&1) ret *= a;
a *= a;
b >>= 1;
}
return ret;
}
void solve()
{
int tmp = m;
for(int i = 2; i <= tmp/i; ++i)
{
if(tmp%i == 0)
{
p[plen++] = i;
while(tmp%i == 0) tmp /= i;
}
}
if(tmp > 1) p[plen++] = tmp;
LL res = 0;
LL M = m;
for(int i = 1; i < (1<<plen); ++i)
{
LL mult = 1;
int cnt = 0;
for(int j = 0; j < plen; ++j)
{
if(i&(1<<j))
{
++cnt;
mult *= p[j];
}
}
if(cnt&1) res += pow(M/mult,n);
else res -= pow(M/mult,n);
}
cout << pow(M,n)-res << endl;
}
int main()
{
cin >> n >> m;
solve();
return 0;
}