Law of Commutation HDU - 6189
As we all know, operation ''+'' complies with the commutative law. That is, if we arbitrarily select two integers
a and
b
+b always equals to
+a. However, as for exponentiation, such law may be wrong. In this problem, let us consider a modular exponentiation. Give an integer
n and an integer
a
a, count the number of integers
b in the range of
,m] which satisfy the equation
a
a (mod
m).
There are no more than
test cases.
Input
Each test case contains two positive integers
and a seperated by one space in a line.
For all test cases, you can assume that
n≤30,1≤a≤1
n≤30,1≤a≤109.
Output
For each test case, output an integer denoting the number of
Sample Input
2 3
2 2
Sample Output
1
2
题意: 略。
思路: 打表+ 不放弃找规律+ 稍微有点数学意识。
比赛的时候打了个表,现在看了好像打表的时候就打错了,不过还是接着找了一下规律:
a 为奇数的时候 只有 1 ,对于偶数情况,打表发现 b —> n 的情况 比较混乱,似乎没有规律,就 放弃了......
(大概这就是蒻~吧)
正解: 对于偶数情况 (a,b都为偶数) b<n 的情况比较混乱,直接暴力枚举, b>=n 的时候就可以有公式可循了。
其实仔细想想也是这样,即 考虑 a^b 大概只有 a^b % 2^n == 0 的时候才有解,要保证 a^b 存在 2^n 即 b>n ,
其实是瞎解释的,可以忽略。
那么b>= n 的时候:~~~~ 点击打开链接
#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include <bits/stdc++.h>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<algorithm>
#define maxn 10010
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MOD 1000000007
#define ll long long
using namespace std;
ll Pow(ll a,ll b,ll mod)
{
ll ans=1;
a=a%mod;
while(b)
{
if(b&1)
ans=ans*a%mod;
b>>=1;
a=a*a%mod;
}
return ans%mod;
}
ll Pow1(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)
ans*=a;
b>>=1;
a=a*a;
}
return ans;
}
int main()
{
ll n,a;
while(scanf("%lld%lld",&n,&a)!=EOF)
{
if(a&1)
{
puts("1");
continue;
}
ll mod=Pow1(2,n);
ll t=Pow1(2,(n-1)/a+1);
ll z=mod/t-(n-1)/t;
ll ans=0;
for(int i=1;i<n;i++)
{
if(Pow(a,i,mod)==Pow(i,a,mod))
ans++;
}
ans+=z;
cout<<ans<<endl;
}
return 0;
}