Happy Equation 题解(数论)

题目链接

题目思路

这个题目好像一个假题啊,本以为是什么高深算法,其实很简单

打表发现\(a\)为奇数答案就是\(1\)

\(a\)为偶数进行分类讨论

\(x\leq p\)进行暴力\(check\)

\(p\le x\) 发现\(a^x\%2^p=0\)

那么只需要找有多少个\(p\le x\;x^a\%2^p=0\)

其实就是找\(2^p\)里面有多少个\(2^\frac{p}{a}\)

然后在减去\(p\)中有多少个\(2^\frac{p}{a}\)

代码

#include<bits/stdc++.h>
#define fi first
#define se second
#define debug cout<<"I AM HERE"<<endl;
using namespace std;
typedef long long ll;
const int maxn=1e2+5,inf=0x3f3f3f3f;
const double eps=1e-6;
int a,p,mod;
ll qpow(ll a,ll b){
    ll ans=1,base=a;
    while(b){
        if(b&1) ans=ans*base%mod;
        base=base*base%mod;
        b>>=1;
    }
    return ans;
}
signed main(){
    int _;scanf("%d",&_);
    while(_--){
        scanf("%d%d",&a,&p);
        if(a%2==1){
            printf("1\n");
            continue;
        }
        mod=(1<<p);
        int ans=0;
        for(int i=1;i<=p;i++){
            if(qpow(i,a)==qpow(a,i)) ans++;
        }
        int tmp=p/a+(p%a==0?0:1);
        ans+=(1<<p)/(1<<tmp)-p/(1<<tmp);
        printf("%d\n",ans);
    }
    return 0;
}

posted @ 2021-12-01 22:01  hunxuewangzi  阅读(83)  评论(0编辑  收藏  举报