本文主要是介绍[HDU 5728] PowMod (欧拉函数的积性+欧拉公式降幂+欧拉筛),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
HDU - 5728
求 K=∑i=1mϕ(i∗n)mod1000000007
其中 n 是 square-free number
求ans=KKKK..modp
先求 K
由于
对于素因子 pk ,如果 i 中不包含这个因子,提出来是
如果 i 中包含这个因子,那么提出来就是
而 1 到
他们除以 pk 后是 1 、
设 K=sum(n,m)=(pk−1)×sum(npk,m)+sum(n,mpk)
递归计算即可,复杂度不会超过 (logN)
再求 ans
利用欧拉定理降幂
ax=ax%ϕ(p)+ϕ(p)modp
递归计算, ϕ(p) 很快就变成 1 了,所以复杂度不会超过
另外在欧拉函数打表那块,由于 N 有
不然容易 TLE (虽然我用埃氏筛没 TLE
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define SQR(a) ((a)*(a))const int maxn=1e7+10, MOD=1000000007;
int N,M,P;
bool nprim[maxn];
int phi[maxn];
int phi_sum[maxn];
int prime[maxn], pcnt;void prime_init();
LL SUM(int,int,vector<int>&);
LL PowMod(LL,LL);
LL Pow(LL,LL,LL);int main()
{#ifdef LOCALfreopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);#endifprime_init();while(~scanf("%d%d%d", &N, &M, &P)){vector<int> fact;int tem=N;for(int i=0; i<pcnt && prime[i]<=tem; i++) if(tem%prime[i]==0){tem/=prime[i];fact.push_back(prime[i]);}LL K = SUM(N,M,fact);printf("%lld\n", PowMod(K,(LL)P));}return 0;
}LL PowMod(LL k, LL p)
{if(p==1) return 0;LL tp=PowMod(k,phi[p]);LL res = Pow(k,tp+phi[p],p);return res;
}LL Pow(LL x, LL n, LL p)
{LL res=1;while(n){if(n&1) res=res*x%p;x=x*x%p;n>>=1;}return res;
}LL SUM(int n, int m, vector<int>& fact)
{if(n==1) return phi_sum[m];if(m==1) return phi[n];if(m<1) return 0;for(int i=0; i<(int)fact.size(); i++) if(n%fact[i]==0)return (SUM( n, m/fact[i], fact) + (LL)(fact[i]-1)*SUM( n/fact[i], m, fact)%MOD)%MOD;return 0;
}void prime_init()
{phi[1]=1;for(int i=2;i<maxn;i++){if(!nprim[i]) {phi[i]=i-1; prime[pcnt++]=i;}for(int j=0;j<pcnt;j++){if(i*prime[j]>=maxn)break;nprim[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);}}}for(int i=1; i<maxn; i++) phi_sum[i] = (phi_sum[i-1]+phi[i])%MOD;}
这篇关于[HDU 5728] PowMod (欧拉函数的积性+欧拉公式降幂+欧拉筛)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!