[数学][欧拉降幂定理]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 de?ned 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思路:当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>
long long n,m;
long long mod;
long long phi(long long x)
{
    long long res=x;
    for(long long i=2; i*i<=x; ++i)
    {
        if(x%i==0)
        {
            res=res-res/i;
            while(x%i==0)
                x/=i;
        }
    }
    if(x>1)
        res=res-res/x;
    return res;
}
long long qpow(long long a,long long n,long long mod)
{
    long long res=1;
    while(n)
    {
        if(n&1)
        {
            res*=a;
            res%=mod;
        }
        n>>=1;
        a=(a*a)%mod;
    }
    return res;
}
long long solve(long long n,long long 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);
    long long tem=phi(m);
    return qpow(n,solve(n-1,tem)+tem,m);
}
int main()
{
    while(~scanf("%lld%lld",&n,&m))
    {
        printf("%lld\n",solve(n,m));
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lllxq/p/9748184.html

时间: 2024-08-28 23:05:48

[数学][欧拉降幂定理]Exponial的相关文章

HDU4704(SummerTrainingDay04-A 欧拉降幂公式)

Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3245    Accepted Submission(s): 1332 Problem Description Sample Input 2 Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input fil

ACM-数论-广义欧拉降幂

https://www.cnblogs.com/31415926535x/p/11448002.html 曾今一时的懒,造就今日的泪 记得半年前去武大参加的省赛,当时的A题就是一个广义欧拉降幂的板子题,后来回来补了一下,因为没有交的地方,于是就测了数据就把代码扔了,,,然后,,昨天的南京网络赛就炸了,,,一样的广义欧拉降幂的板子题,,然后因为忘记了当初自己想出来的那中写法,,一直想着回想起之前的写法,,然后到结束都没弄出来,,,emmmm,, 赛后看了一下别人的解法,,别人的处理方法很巧妙,,当

bzoj3884: 上帝与集合的正确用法 欧拉降幂公式

欧拉降幂公式:http://blog.csdn.net/acdreamers/article/details/8236942 糖教题解处:http://blog.csdn.net/skywalkert/article/details/43955611 注:知道欧拉公式是远远不够的,还要知道欧拉降幂公式,因为当指数很大的时候需要用 然后欧拉降幂公式不要求A,C互质,但是B必须大于等于C的欧拉函数 吐槽:感觉记忆化搜索影响不大啊,当然肯定是因为太水了 这样复杂度是O(T*sqrt(p)*logp)

【学习总结】数学-欧拉函数

定义 欧拉函数f(n)表示小于n并且与n互质的数的个数 f(n)=n(1?1p1)(1?1p2)-(1?1pk)(pi为n的质因子) 代码 C++ 单个处理 int eulerPhi(int n) { int m = (int)sqrt(n+0.5); in ans = n; for (int i = 2; i <= m; i++) { if (n % i == 0) { ans = ans / i * (i-1); while (n%i==0) n /= i; } } if (n > 1)

CodeForces 906D Power Tower &lt;&lt;欧拉降幂

题意 给定n个数,q次询问,每次输出[l,r]区间的超级幂,对m取模. 思路 超级幂问题就想到用欧拉降幂来处理 欧拉降幂公式:$a^b \% m=a^{b\%\varphi (m)+\varphi(m)}\%m,(b>\varphi(m))$ 本题用递归处理欧拉降幂,在$logm$次降幂后$\varphi(m)=1$,然后回溯时用快速幂进行计算,总的复杂度大约是$log^{2}m$ $w_0^{w_1^{w_2^{w_3^{...}}}}\% m = w_0^{[w_1^{w_2^{w_3^{.

Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)

传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\end{aligned} \] 思路 我们通过迭代发现\(f_n\)其实就是由\(c^{x_1},f_1^{x_2},f_2^{x_3},f_3^{x_4}\)相乘得到,因此我们可以分别用矩阵快速幂求出\(x_1,x_2,x_3,x_4\),最后用快速幂求得答案. 对\(f_1,f_2,f_3\): \[ \begin{aligned} (x_n&&

FZU 1759 题解 欧拉降幂

本题考点:欧拉降幂 Super A^B mod C Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000). Input There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separa

super_log (广义欧拉降幂)(2019南京网络赛)

题目: In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For example, the complexity of a typical disjoint set is O(nα(n))O(nα(n)). Here α(n)α(n) is Inverse Ackermann Function, which growth speed is very slow. So

2019ICPC网赛南京站B题 super_log(欧拉降幂

https://nanti.jisuanke.com/t/41299 题意:让算a^(a^(a^(...))),一共b个a, (mod p)的结果. 思路:这是个幂塔函数,用欧拉降幂公式递归求解. #include<bits/stdc++.h> #define ll long long using namespace std; map<int,int> euler; ll a,b,mod; int phi(int n) { int now=n; int ret=n; if(eule