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, separated by a single space.

Output

For each testcase, output an integer, denotes the result of A^B mod C.

Sample Input

3 2 4

2 10 1000

Sample Output

1

23

分析:欧拉降幂的模板题

直接套公式即可:

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
ll a,p;
char b[10000005];
ll phi(ll x)
{
    ll res=x;
    ll a=x;
    for(int i=2;i*i<=x;i++)
    {
        if(a%i==0)
        {
            res=res/i*(i-1);
            while(a%i==0)    a/=i;
        }
    }
    if(a>1)    res=res/a*(a-1);
    return res;
}
ll qp(ll a,ll b,ll p)
{
    ll ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=(ans*a)%p;
        }
        a=(a*a)%p;
        b>>=1;
    }
    return ans;
}
int main()
{
    //freopen("input.txt","r",stdin);
    while(scanf("%lld %s %lld",&a,&b,&p)!=EOF)
    {
        ll len=strlen(b);
        ll res=0;
        ll pp=phi(p);
        for(int i=0;i<len;i++)
        {
            res=(res*10+b[i]-‘0‘)%pp;
        }
        printf("%lld\n",qp(a,res,p));
    }
    return 0;
}

原文地址:https://www.cnblogs.com/cautx/p/11396842.html

时间: 2024-07-30 16:57:07

FZU 1759 题解 欧拉降幂的相关文章

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)

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

[数学][欧拉降幂定理]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 nu

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&&

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

ACM-数论-广义欧拉降幂

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

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

The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest C. Xyjj’s sequence(动态规划+欧拉降幂)

题目链接:https://nanti.jisuanke.com/t/40255 中文题面: 解题思路:先用欧拉降幂求出A,B两个序列,定义dp[0][i][j]为取A的前i个元素,B的前j个元素,且C的最后一个元素为B[j],同理dp[1][i][j]为取A的前i个元素,B的前j个元素,且C的最后一个元素为A[i],那么就很容易得到状态转移方程.那么最后答案即为max(dp[0][n][n],dp[1][n][n]).还有值得注意的是:该题需要使用滚动数组,不然会超内存. 在此贴两个关于欧拉降幂