码迷,mamicode.com
首页 > 其他好文 > 详细

扩展欧拉降幂

时间:2020-01-26 20:38:45      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:lse   typedef   exp   name   map   clu   解法   set   int   

https://nanti.jisuanke.com/t/41299

题意:给出a , b , m . 求a的a次方b次取模后的值。

解法:拓展欧拉降幂,递归求解。

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 20191117
#define PI acos(-1)
using namespace std;
typedef long long ll ;



ll quickpow(ll a ,  ll b , ll mo)
{
    ll ans = 1 ;
    while(b)
    {
        if(b&1)
            ans = ans * a % mo ;
        a = a * a % mo ;
        b >>= 1 ;
    }
    return ans % mo ;
}

ll get_phi(ll x)
{
    ll ans = x ;
    for(int i = 2 ; i * i <= x ; i++)
    {
        if(x % i == 0)
        {
            ans = ans - ans / i ;
            while(x % i == 0)
                x = x / i ;
        }
    }
    if(x > 1) ans = ans - ans / x ;
    return ans ;
}

ll cal(ll a , ll b , ll m)
{
    if(m == 1) return 0 ;
    if(b == 0) return 1 ;
    ll p = get_phi(m);
    ll exp = cal(a , b - 1 , p);
    if(exp < p || exp)//这个判断条件,还不是很清楚为什么?
        return quickpow(a , exp , m);
    else
        return quickpow(a , exp+p , m);
}

int main()
{
    int t ;
    scanf("%d" , &t);
    while(t--)
    {
        ll a , b , m ;
        scanf("%lld%lld%lld" , &a , &b , &m);
        cout << cal(a , b , m)%m << endl;
    }


    return 0;
}

 

扩展欧拉降幂

标签:lse   typedef   exp   name   map   clu   解法   set   int   

原文地址:https://www.cnblogs.com/nonames/p/12234619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!