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