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;
}

 

 

### HDU OJ Problem 2566 Coin Counting Solution Using Simple Enumeration and Generating Function Algorithm #### 使用简单枚举求解硬币计数问题 对于简单的枚举方法,可以通过遍历所有可能的组合方式来计算给定面额下的不同硬币组合数量。这种方法虽然直观但效率较低,在处理较大数值时性能不佳。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] coins = {1, 2, 5}; // 定义可用的硬币种类 while (scanner.hasNext()) { int targetAmount = scanner.nextInt(); int countWays = findNumberOfCombinations(targetAmount, coins); System.out.println(countWays); } } private static int findNumberOfCombinations(int amount, int[] denominations) { if (amount == 0) return 1; if (amount < 0 || denominations.length == 0) return 0; // 不使用当前面值的情况 int excludeCurrentDenomination = findNumberOfCombinations(amount, subArray(denominations)); // 使用当前面值的情况 int includeCurrentDenomination = findNumberOfCombinations(amount - denominations[0], denominations); return excludeCurrentDenomination + includeCurrentDenomination; } private static int[] subArray(int[] array) { if (array.length <= 1) return new int[]{}; return java.util.Arrays.copyOfRange(array, 1, array.length); } } ``` 此代码实现了通过递归来穷尽每一种可能性并累加结果的方式到满足条件的不同组合数目[^2]。 #### 利用母函数解决硬币计数问题 根据定义,可以将离散序列中的每一个元素映射到幂级数的一个项上,并利用这些多项式的乘积表示不同的组合情况。具体来说: 设 \( f(x)=\sum_{i=0}^{+\infty}{a_i*x^i}\),其中\( a_i \)代表当总金额为 i 时能够组成的方案总数,则有如下表达式: \[f_1(x)=(1+x+x^2+...)\] 这实际上是一个几何级数,其封闭形式可写作: \[f_1(x)=\frac{1}{(1-x)}\] 同理,对于其他类型的硬币也存在类似的生成函数。因此整个系统的生成函数就是各个单独部分之积: \[F(x)=f_1(x)*f_2(x)...*f_n(x)\] 最终目标是从 F(x) 中提取系数即得到所需的结果。下面给出基于上述理论的具体实现: ```cpp #include<iostream> using namespace std; const int MAXN = 1e4 + 5; int dp[MAXN]; void solve() { memset(dp, 0, sizeof(dp)); dp[0] = 1; // 初始化基础状态 int values[] = {1, 2, 5}, size = 3; for (int j = 0; j < size; ++j){ for (int k = values[j]; k <= 10000; ++k){ dp[k] += dp[k-values[j]]; } } } int main(){ solve(); int T; cin >> T; while(T--){ int n; cin>>n; cout<<dp[n]<<endl; } return 0; } ``` 这段 C++ 程序展示了如何应用动态规划技巧以及生成函数的概念高效地解决问题实例[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

心脏dance

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

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

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

打赏作者

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

抵扣说明:

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

余额充值