题目大意:
给出a,n,求有多少个正整数b满足 ab≡ba (mod 2n ),( 1≤n≤30 , 1≤a≤109 );
解题思路:
再一次认识到了打表找规律的重要性。
显然 a,b 奇偶要相同。
对于
a
为奇数的情况,打表就可以发现,
而对于a为偶数的情况,b也一定是偶数。
若
b≥n
,则
ab≡0
(mod
2n
),所以
ba≡0
(mod
2n
),设
b=(2k∗c
)(
c
为奇数),则
若
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;
}