hdu6189 Law of Commutation 2017ACM-ICPC全国邀请赛(广西) 找规律

链接:http://acm.hdu.edu.cn/showproblem.php?pid=6189
先贴一波官方题解:

>

I Law of Commutation
对于a为奇数的情况,b一定为奇数,下证b=a mod 2^n。
由于奇数平方模8余1,故a^b=a mod 8, b^a=b mod 8
故a=b mod 8
由于奇数四次方模16余1,故a^b=a^(b%4) mod 16, b^a=b^(a%4) mod 16
由于b%4=a%4,故a=b mod 16
以此类推,得b=a mod 2^n。解唯一(当然打表可以看出来,现场一堆打表过的)
对于a为偶数的情况,b一定为偶数。
若b≥n,则a^b=0 mod 2^n,故b^a=0 mod 2^n。
可直接求出b≥n时的个数。
若b<n,则直接暴力,时间复杂度O(nloga)。

#include <iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 6;
const int mod = 1000000007 ;
int qmod(int a,int b,int mod)
{
    int r=1;
    for (a%=mod; b; b/=2,a=1ll*a*a%mod)
        if (b&1)
            r=1ll*r*a%mod;
    return r;
}
int main()
{
    int a,n;
    while( cin>>n>>a)
    {

        int m = 1;
        for(int i=1; i<=n; i++)
        {
            m*=2;
        }
        /* cout<<"m: "<<m<<endl;//打表程序
        int o = 0;
        for(int i=1;i<=m;i++)
        {
          if(i<=n&&qmod(a,i,m)==qmod(i,a,m))
          {
              printf("%d\n",i);
              o++;
          }
          else if(qmod(a,i,m)==qmod(i,a,m))
          {
              printf("@%d\n",i);
              o++;
          }
        }
        cout<<o<<endl;*/
        if(a&1)
        {
            puts("1");//打表可知a为奇数时,一定只有一个
        }
        else
        {
            int t = 1<<((n-1)/a+1);
 //t的计算是2^{(n-1)/a+1},目的是计算b>n时的符合条  件的两个偶数之间的差,举三个例子           
//  n a t,  25 8 -》16 ,
//  24 8 -》8 , 18 8 -》8
            int u = m/t-(n-1)/t;//已经带上了等于n的情况
            int o = 0;
            for(int i=1; i<n; i++)
            {
                if(qmod(a,i,m)==qmod(i,a,m))
                {
                    o++;
                }
            }
            o+=u;
            printf("%d\n",o);
        }

    }
    return 0;
}

参考博客:
http://blog.csdn.net/cww97/article/details/77753648

小tricks:
暴力打表的过程中,想到了一直纠结的pow精度问题,于是百度了一发,原来是因为pow模板函数里正好没有int,int双整形参数的定义,要想用的话只能用double double型

【C++】关于pow函数的用法
在C++中,pow有多个重载函数;
在dev中,pow(int,int)可以执行,但是在别的地方是不可以被编译的;会提示 :error C2668: “pow”: 对重载函数的调用不明确

这里写图片描述

可以看见,是没有pow(int,int)这个方法的;
正确的办法是pow(int,double(int));

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值