hdu 2879【留坑】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sxy201658506207/article/details/83580841

看了题解,说的都不太准确,可能是积性函数是为莫比乌斯准备的吧,莫比乌斯真的难

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 10000005;
const int M = 1000005;
bool prime[N];
int p[N];
int k;
LL mod;
void isprime()
{
    k = 0;
    memset(prime,true,sizeof(prime));
    for(int i=2;i<N;i++)
    {
        if(prime[i])
        {
            p[k++] = i;
            for(int j=i+i;j<N;j+=i)
                prime[j] = false;
        }
    }
}
LL quick_mod(LL a,LL b)
{
    LL ans = 1;
    a %= mod;
    while(b)
    {
        if(b&1)
        {
            ans = ans * a % mod;
            b--;
        }
        b >>= 1;
        a = a * a % mod;
    }
    return ans;
}
int main()
{
    int n,m,t;
    isprime();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&mod);
        LL cnt = 0;
        for(int i=0;i < k;i++)
        {
            if(p[i] > n) break;
            cnt += n / p[i];
        }
        LL ans = quick_mod(2,cnt);
        printf("%I64d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sxy201658506207/article/details/83580841
hdu