Law of Commutation HDU - 6189


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
n30,1a1  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

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







 

      


   







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值