zoukankan      html  css  js  c++  java
  • [数学][广义欧拉定理]Exponial

     

    题目描述

    Illustration of exponial(3) (not to scale), Picture by C.M. de Talleyrand-Périgord via Wikimedia Commons Everybody loves big numbers (if you do not, you might want to stop reading at this point). There are many ways of constructing really big numbers known to humankind, for instance:
    In this problem we look at their lesser-known love-child the exponial , which is an operation defined for all positive integers n as
    For example, exponial(1) = 1 and  which is already pretty big. Note that exponentiation is right-associative:  .
    Since the exponials are really big, they can be a bit unwieldy to work with. Therefore we would like you to write a program which computes exponial(n) mod m (the remainder of exponial(n) when dividing by m).

    输入

    The input consists of two integers n (1 ≤ n ≤ 109 ) and m (1 ≤ m ≤ 109 ).

    输出

    Output a single integer, the value of exponial(n) mod m.

    样例输入

    2 42
    

    样例输出

    2
    欧拉降幂定理:当b>phi(p)时,有a^b%p=a^(b%phi(p)+phi(p))%p
    https://blog.csdn.net/weixin_38686780/article/details/81272848
    思路:当n>=6时,欧拉降幂定理一定适用,因为f(5)>1e9,也就是一定有欧拉降幂定理的b>phi(p)这个条件,所以f(n)%p=n^f(n-1)%p=n^(f(n-1)%phi(p)+phi(p))%p;再递归地求f(n-1)%phi(p)
    当n<=5时,f(n)%p=n^f(n-1)%p,因为不一定有f(n-1)>phi(p)成立,所以不能用欧拉降幂定理求,直接手动求出f(n)%p即可;
    从1e9递归到5很慢,但当p=1时,可以直接返回f(n)%p=0而不用递归到下一层;
    AC代码:
    #include <cstdio>
    typedef long long ll;
    
    ll phi(ll x){
        ll ret=x;
        for(ll i=2;i*i<=x;++i){
            if(x%i==0){
                ret=ret-ret/i;
                while(x%i==0) x/=i;
            }
        }
        if(x>1) ret=ret-ret/x;
        return ret;
    }
    ll qpow(ll a,ll b,ll mod){
        ll ret=1;
        while(b){
          if(b&1) ret=ret*a%mod;
          a=a*a%mod;
          b>>=1;
        }
        return ret;
    }
    ll solve(ll n,ll m){
        if(m==1) return 0;
        if(n==1) return 1;
        else if(n==2) return 2%m;
        else if(n==3) return 9%m;
        else if(n==4) return qpow(4,9,m);
        else if(n==5) return qpow(5,262144,m);//可以舍去,不知道为啥
        ll tem=phi(m);
        return qpow(n,solve(n-1,tem)+tem,m);
    }
    int main()
    {
        ll n,m;
        while(scanf("%lld%lld",&n,&m)!=EOF){
            printf("%lld
    ",solve(n,m));
        }
        return 0;
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    HTML5/CSS3滑块动画菜单
    基于HTML5手机登录注册表单代码
    基于HTML5手机上下滑动翻页特效
    基于jQuery+HTML5页面整屏滑动切换代码
    基于html5可拖拽图片循环滚动切换
    基于html5背景图片自适应代码是一款背景不随滚动条滚动,会根据分辨率不同自动匹配对应的背景图片
    HTML5实现摇一摇
    html5桌面通知,notification的使用,右下角出现通知框
    html5全局属性
    HTML5 QQ登录背景动态图片
  • 原文地址:https://www.cnblogs.com/lllxq/p/9748184.html
Copyright © 2011-2022 走看看