积性函数:
函数f如果满足对于任意两个互质的正整数m和n,均有f(mn)=f(m)f(n),就称f为积性函数(或乘性函数)。如果对于任意两个正整数m和n,均有f(mn)=f(m)f(n),就称为完全积性函数。
因数和函数:
费马小定理应用
因数和函数为积性函数,结合费马小定理
试除法
#include<iostream>
#include<cmath>
using namespace std;
void primeFactor(int n){
//求解n的素因数
while(n%2==0){
cout<<2<<" ";
n/=2;
}
for(int i=3;i<=sqrt(n*1.0);i+=2){
while(n%i==0){
cout<<i<<" ";
n/=i;
}
}
if(n>2){
cout<<n;
}
cout<<endl;
}
int main(){
int n;
cin>>n;
primeFactor(n);
return 0;
}
筛选法(试除plus)
就是在试除的基础上限定处理范围,提前限定打表减少无关数据
int prime[N];/// 存储素数
int nprime;///记录素数的个数
bool isprime[N];
void make_prime()
{
int i,j;
nprime=0;
memset(isprime,1,sizeof(isprime));
isprime[1]=0;
for(i=2;i<=sqrt(N);i++)
{
if(isprime[i])
{
nprime++;
prime[nprime]=i;
for(j=i*i;j<N;j+=i)
{
isprime[j]=0;
}
}
}
}
void divide(int n)
{
int i,count=0;
int factor[N];
int temp=sqrt(n+0.0);
for(i=1;i<=nprime;i++)
{
if(prime[i]>temp)
break;
while(n%prime[i]==0)
{
count++;
factor[count]=prime[i];
n/=prime[i];
}
}
if(n!=1)
{
count++;
factor[count]=n;
}
}
gcd幂次函数分解
#include<cstdio>
typedef long long LL;
LL gcd(LL a,LL b){
return b==0?a:gcd(b,a%b);
}
LL pp(LL a,LL m,LL mod)
{
LL ans=1;
while(m){
if(m&1){
ans=(ans*a)%mod;
}
a=a*a%mod;
m>>=1;
}
return ans;
}
int main()
{
int t;
LL a,m,n,k;
scanf("%d",&t);
while(t--){
scanf("%lld%lld%lld%lld",&a,&m,&n,&k);
LL s1=gcd(m,n);
LL s=(pp(a,s1,k)-1+k)%k;
printf("%lld\n",s);
}
return 0;
}
来源:CSDN
作者:Excalibur__C
链接:https://blog.csdn.net/qq_43658924/article/details/103581701