HDU——6189 Law of Commutation(数论+打表找规律)

As we all know, operation ''+'' complies with the commutative law. That is, if we arbitrarily select two integers aa and bb, a+ba+b always equals to b+ab+a. However, as for exponentiation, such law may be wrong. In this problem, let us consider a modular exponentiation. Give an integer m=2nm=2n and an integer aa, count the number of integers bb in the range of [1,m][1,m] which satisfy the equation ab≡baab≡ba (mod mm).

Input

There are no more than 25002500 test cases. 

Each test case contains two positive integers nn and a seperated by one space in a line. 

For all test cases, you can assume that n≤30,1≤a≤109n≤30,1≤a≤109.

Output

For each test case, output an integer denoting the number of bb. 

Sample Input

2 3
2 2

Sample Output

1
2

题意:题意:给定n,a,其中令2^n=m,现求区间[1,m]内有多少个b满足,a^b%m=b^a%m。

题解:打表发现a为奇数时,答案肯定为1,这个规律很容易看出,然后a为偶数时,m一定为偶数(因为m是2^n),所以a^b%m的结果也为偶数,那么b^a%m的结果也必须为偶数,所以b必须为偶数,a为偶数那么一定可以写成2*x(构造的一种方法),所以a^b=2^b*x^b,又m=2^n,所以可以发现如果 b>=n 那么a^b%m一定为0(带进去就可以发现),这个时候就就需要分类讨论,由于题目数据中n非常小,所以b<=n时直接暴力求解(带着等号是因为我们最后需要减去n/minn,待会看代码),那么b>n时,我们已经知道a^b%m=0所以我们需要b^a%m同样为0,因为b为偶数,那么b一定可以表示为2^x*y(这个构造很奇妙,也很好理解,不过不好想),那么b^a=2^ax*y^a,我们令y取得最小值1,b^a=2^ax,b^a(2^ax)%m(2^n)=0(b^a%m),所以需要ax>=n,解得x>=n/a(注意:这里n/a应该向上取整,向下取整会造成ax<n的情况),所以我们就这样找到最小满足条件的x(这样求出来的x是最小的,读者自行化简上面的式子,可令y为任何值,化简,求出最小值的结果是一样的)),然后求出1~m以内2^x的倍数即可(注意把n以内的答案减去,因为n以内的答案暴力时求过一遍,即:暴力求的结果包括a^b%m=偶数的结果,包括了a^b%m=0的结果)。

上代码:

打表代码:

#include <iostream>
using namespace std;
typedef long long ll;
ll n,z;
ll m;
ll quick(ll a,ll b){
	ll ans=1;
	while(b){
		if(b&1) ans=(ans*a)%m;
		b>>=1;
		a=(a*a)%m;
	}
	return ans;
}
int main(){
    //i为n,j为a
	for (int i = 1; i <= 20;i++){
		for (int j = 2; j <= 30;j++){
			ll ans=1;
			ll z=j;
			for (int r = 1; r <= i;r++){
				ans=ans*2ll;
			}
			m=ans;
			ll sum=0;
			for (int k = 1; k <= ans;k++){
				if(quick(z,k)==quick(k,z)){
					sum++;
				}
			}
			cout << i << " " << j << " " << sum << endl;//i为n,j为a
		}
	}
	return 0;
}

正解代码:

#include <iostream>
using namespace std;
typedef long long ll;
ll n,a,m;
ll quick(ll x,ll y){
	ll ans=1;
	while(y){
		if(y&1) ans=ans*x;
		y>>=1;
		x=x*x;
	}
	return ans;
}
ll quick(ll x,ll y,ll m){
	ll ans=1;
	while(y){
		if(y&1) ans=(ans*x)%m;
		y>>=1;
		x=(x*x)%m;
	}
	return ans;
}
int main(){
	while(cin >> n >> a){
		if(a&1){//a为奇数
			cout << 1 << endl;
			continue;
		}
		m=(1ll<<n);
		ll sum=0;
		for (int b = 1; b <= n;b++){//暴力求解<=n的
			if(quick(a,b,m)==quick(b,a,m)){
				sum++;
			}
		}
		ll minx=n/a;//最小x
		if(minx*a<n) minx++;//向上取整
		ll minn=quick(2ll,minx);//最小2^x
		ll ans=m/minn-n/minn+sum;//这里减去n/minn所以暴力求解包括n的值,因为n也可能是
		cout << ans << endl;
	}
	return 0;
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值