hdu5728PowMod+欧拉函数和+k的无穷次方取膜

本文介绍了一种计算特定形式幂次表达式的模运算的方法,利用欧拉函数的性质来简化计算过程,并通过素数筛选和快速幂运算提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
Declare:
k=mi=1φ(in)mod1000000007

n is a square-free number.

φ is the Euler’s totient function.

find:
ans= kkkk...k mod p

There are infinite number of k

Input
Multiple test cases(test cases ≤100), one line per case.

Each line contains three integers, n,m and p.

1≤n,m,p≤107

Output
For each case, output a single line with one integer, ans.

Sample Input

1 2 6
1 100 9

Sample Output

4
7

Author
HIT

Source
2016 Multi-University Training Contest 1

这里写图片描述

证明参考这位菊苣的吧http://blog.youkuaiyun.com/wust_zzwh/article/details/51966450
后边那个k的无穷个k的那个参考八中oj3884
http://blog.youkuaiyun.com/xtulollipop/article/details/52065860

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
using namespace std;

#define LL long long

const int mod=1000000007;
const int maxn=10000005;
bool check[maxn];          //用于打表记录的中间量
LL sumPhi[maxn];           //前i个的欧拉函数和
int cnt,phi[maxn],prime[maxn];  //素数个数,欧拉表,素数表
//素数表是第几个素数是什么,欧拉表是i的欧拉是phi[i];
void init(){               //素数+欧拉表
    phi[1]=1;
    cnt=0;
    for(int i=2;i<maxn;i++){
        if(!check[i]){
            phi[i]=i-1;
            prime[cnt++]=i;
        }
        for(int j=0;j<cnt;j++){
            if(i*prime[j]>=maxn)break;
            check[i*prime[j]]=true;
            if(i%prime[j]==0){
                phi[i*prime[j]]=phi[i]*prime[j];
                break;
            }
            else{
                phi[i*prime[j]]=phi[i]*(prime[j]-1);
            }
        }
    }
    sumPhi[0]=0;
    for(int i=1;i<maxn;i++) sumPhi[i]=(sumPhi[i-1]+phi[i])%mod;
}
LL phiz(int n,int m){       //计算k,
    if(n==1) return sumPhi[m];
    if(m==1) return phi[n];
    if(m<1) return 0;

    for(int i=0;i<cnt;i++){
        if(n%prime[i]==0){  //n的最小质数prime[i]
            LL temp1=(prime[i]-1)*phiz(n/prime[i],m)%mod;
            LL temp2=phiz(n,m/prime[i])%mod;
            return (temp1+temp2)%mod;
        }
    }
    return 0;
}
LL pow(LL a,LL b,int p){  //快速幂
    LL ret=1;
    a%=p;
    while(b){
        if(b&1) ret=(ret*a)%p;
        a=(a*a)%p;
        b/=2;
    }
    return ret;
}
LL powe(LL k,int p){  //k^b%p
    if(p==1) return 0;
    LL temp=powe(k,phi[p]);
    return pow(k,temp+phi[p],p);
}
int main(){
    init();
    int n,m,p;
    while(scanf("%d %d %d",&n,&m,&p)!=EOF){
        LL k=phiz(n,m);
        printf("%I64d\n",powe(k,p));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值