NOIP模拟 Math 【同余】

题目大意:

给出a,n,求有多少个正整数b满足 abba (mod 2n ),( 1n30 1a109 );

解题思路:

再一次认识到了打表找规律的重要性。

显然 ab 奇偶要相同。

对于 a 为奇数的情况,打表就可以发现,b唯一( b=a ba (mod 2n ))。

而对于a为偶数的情况,b也一定是偶数。
bn ,则 ab0 (mod 2n ),所以 ba0 (mod 2n ),设 b=(2kc )( c 为奇数),则(2kc)a0(mod 2n) ,所以 2ka2n ,所以 kank(n+a1)/a ,所以b一定是 2(n+a1)/a 的倍数。
b<n ,直接枚举快速幂即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#define ll long long
using namespace std;

int getint()
{
    int i=0,f=1;char c;
    for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());
    if(c=='-')f=-1,c=getchar();
    for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
    return i*f;
}

int T,n;

int ksm(int x,int y)
{
    int res=1;
    for(;y;y>>=1,x=1ll*x*x%(1<<n))
        if(y&1)res=1ll*res*x%(1<<n);
    return res;
}

int main()
{
    //freopen("math.in","r",stdin);
    //freopen("math.out","w",stdout);
    int a,b;
    T=getint();
    while(T--)
    {
        a=getint(),n=getint();
        if(a&1)
        {
            puts("1");
            continue;
        }
        int ans=0;
        for(b=1;b<=n;b++)
            if(ksm(b,a)==ksm(a,b))ans++;
        int k=(n+a-1)/a;
        ans+=((1<<n)>>k)-(n>>k);
        cout<<ans<<'\n';
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值