进阶算法计划【后期 / 夏秋 / 2023】TG13 数论笔记
B3716 分解质因子 3
link
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e8+1;
bitset<N>vis;
int n,T,p[N/10],mn[N];
void pre()
{
for(int i=2;i<N;i++)
{
if(!vis[i]) p[++p[0]]=i,mn[i]=i;
for(int j=1;j<=p[0]&&1ll*i*p[j]<N;j++)
{
vis[i*p[j]]=1;
mn[i*p[j]]=p[j];
if(i%p[j]==0) break;
}
}
}
int main()
{
pre();
ios::sync_with_stdio(0);
cin>>T;
while(T--)
{
int x;cin>>x;
int ans=0;
while(x>1) ans^=mn[x],x/=mn[x];
cout<<ans<<"\n";
}
}
威尔逊定理
欧拉函数
定义:
i为质数时:
线性筛求欧拉函数:
const int N=1e8+1;
bitset<N>vis;
int n,T,p[N/10],phi[N];
void pre()
{
phi[1]=1;
for(int i=2;i<N;i++)
{
if(!vis[i]) p[++p[0]]=i,phi[i]=i-1;
for(int j=1;j<=p[0]&&1ll*i*p[j]<N;j++)
{
vis[i*p[j]]=1;
if(i%p[j]==0)
{
phi[i*p[j]]=phi[i]*p[j];
break;
}
else
phi[i*p[j]]=phi[i]*(p[j]-1);
}
}
}
逆元
定义:
定理:
定理: 模
定理: 在模质数
定理: 一个数的逆元的逆元等于它自身
exgcd求逆元(p可以不为质数):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void exgcd(ll a,ll b,ll &x,ll &y)
{
if(a%b==0) x=0,y=1;
else exgcd(b,a%b,y,x),y-=a/b*x;
}
int main()
{
ll a,b,x,y;
cin>>a>>b;
exgcd(a,b,x,y);
cout<<(x%b+b)%b;
}
欧拉定理: 对于任意正整数
当
费马小定理: 对任意正整数
P2613:
link
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int p = 19260817;
int a, b;
char nc;
#define gc getchar
int read() {
int x = 0;
while (nc = gc(), (nc < 48 || nc > 57) && nc != EOF);
if (nc < 0)
return nc;
x = nc - 48;
while (nc = gc(), 47 < nc && nc < 58 && nc != EOF)
x = (x << 3) + (x << 1) + (nc ^ 48), x %= p;
return x;
}
int qpow(int a, int b) {
int c = 1;
for (; b; b >>= 1, a = 1ll * a * a % p)
if (b & 1)
c = 1ll * a * c % p;
return c;
}
int main()
{
a = read();
b = read();
cout << 1ll * a * qpow(b, p - 2) % p << endl;
}
定理
设
B3717:
link
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define in inline
namespace fast_io
{
char buf[1<<12],*p1=buf,*p2=buf,sr[1<<23],z[23],nc;int C=-1,Z=0,Bi=0,ny;
in char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
in int read()
{
int x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;Bi=1;if(nc<0)return nc;
x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48),Bi++;return x*ny;
}
in int gf() {int a=read(),y=ny,b=(nc!='.')?0:read();return (b?a+(db)b/pow(10,Bi)*y:a);}
in int gs(char *s) {char c,*t=s;while(c=gc(),c<32);*s++=c;while(c=gc(),c>=32)*s++=c;return s-t;}
in void ot() {fwrite(sr,1,C+1,stdout);C=-1;}
in void flush() {if(C>1<<22) ot();}
template <typename T>
in void write(T x,char t)
{
int y=0;if(x<0)y=1,x=-x;while(z[++Z]=x%10+48,x/=10);
if(y)z[++Z]='-';while(sr[++C]=z[Z],--Z);sr[++C]=t;flush();
}
in void write(char *s) {int l=strlen(s);for(int i=0;i<l;i++)sr[++C]=*s++;sr[++C]='\n';flush();}
};
using namespace fast_io;
const int N=5e6+5,p=998244353;
int n,m,t,inv[N],fac[N];
int c(int n,int m)
{
return n<m?0:1ll*fac[n]*inv[m]%p*inv[n-m]%p;
}
int main()
{
inv[0]=1,inv[1]=1,fac[0]=1,fac[1]=1;
for(int i=2;i<N;i++) inv[i]=1ll*(p-p/i)*inv[p%i]%p;
for(int i=2;i<N;i++) fac[i]=1ll*fac[i-1]*i%p,inv[i]=1ll*inv[i]*inv[i-1]%p;
t=read();read();
int ans=0;
while(t--)
{
int n=read(),m=read();
ans^=c(n,m);
}
cout<<ans<<endl;
return ot(),0;
}
中国剩余定理
定义:
给定
其中保证
记
则方程组的唯一通解为:
P1495:
link
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define in inline
namespace fast_io
{
char buf[1<<12],*p1=buf,*p2=buf,sr[1<<23],z[23],nc;int C=-1,Z=0,Bi=0,ny;
in char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
in ll read()
{
ll x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;Bi=1;if(nc<0)return nc;
x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48),Bi++;return x*ny;
}
in int gf() {int a=read(),y=ny,b=(nc!='.')?0:read();return (b?a+(db)b/pow(10,Bi)*y:a);}
in int gs(char *s) {char c,*t=s;while(c=gc(),c<32);*s++=c;while(c=gc(),c>=32)*s++=c;return s-t;}
in void ot() {fwrite(sr,1,C+1,stdout);C=-1;}
in void flush() {if(C>1<<22) ot();}
template <typename T>
in void write(T x,char t)
{
int y=0;if(x<0)y=1,x=-x;while(z[++Z]=x%10+48,x/=10);
if(y)z[++Z]='-';while(sr[++C]=z[Z],--Z);sr[++C]=t;flush();
}
in void write(char *s) {int l=strlen(s);for(int i=0;i<l;i++)sr[++C]=*s++;sr[++C]='\n';flush();}
};
using namespace fast_io;
const int N=11;
ll n,M,x,a[N],b[N],m[N];
void exgcd(ll a,ll b,ll &x,ll &y)
{
if(a%b==0) x=0,y=1;
else exgcd(b,a%b,y,x),y-=a/b*x;
}
int inv(ll a,ll p)
{
ll x,y;
exgcd(a,p,x,y);
return (x%p+p)%p;
}
ll mul(ll a,ll b,ll p)
{
ll d=(ll)(a*(long double)b/p+0.5);
ll c=a*b-d*p;
return c<0?c+p:c;
}
int main()
{
n=read();M=1;
for(int i=1;i<=n;i++) a[i]=read(),b[i]=read(),M*=a[i];
for(int i=1;i<=n;i++)
{
m[i]=M/a[i];
x=(x+m[i]*inv(m[i],a[i])%M*b[i]%M)%M;
}
write(x,'\n');
return ot(),0;
}
P3868:
link
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define in inline
namespace fast_io
{
char buf[1<<12],*p1=buf,*p2=buf,sr[1<<23],z[23],nc;int C=-1,Z=0,Bi=0,ny;
in char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
in ll read()
{
ll x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;Bi=1;if(nc<0)return nc;
x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48),Bi++;return x*ny;
}
in int gf() {int a=read(),y=ny,b=(nc!='.')?0:read();return (b?a+(db)b/pow(10,Bi)*y:a);}
in int gs(char *s) {char c,*t=s;while(c=gc(),c<32);*s++=c;while(c=gc(),c>=32)*s++=c;return s-t;}
in void ot() {fwrite(sr,1,C+1,stdout);C=-1;}
in void flush() {if(C>1<<22) ot();}
template <typename T>
in void write(T x,char t)
{
int y=0;if(x<0)y=1,x=-x;while(z[++Z]=x%10+48,x/=10);
if(y)z[++Z]='-';while(sr[++C]=z[Z],--Z);sr[++C]=t;flush();
}
in void write(char *s) {int l=strlen(s);for(int i=0;i<l;i++)sr[++C]=*s++;sr[++C]='\n';flush();}
};
using namespace fast_io;
const int N=11;
ll n,M,x,a[N],b[N],m[N];
void exgcd(ll a,ll b,ll &x,ll &y)
{
if(a%b==0) x=0,y=1;
else exgcd(b,a%b,y,x),y-=a/b*x;
}
int inv(ll a,ll p)
{
ll x,y;
exgcd(a,p,x,y);
return (x%p+p)%p;
}
ll mul(ll a,ll b,ll p)
{
ll d=(ll)(a*(long double)b/p+0.5);
ll c=a*b-d*p;
return c<0?c+p:c;
}
int main()
{
n=read();M=1;
for(int i=1;i<=n;i++) b[i]=read();
for(int i=1;i<=n;i++) a[i]=read(),M*=a[i];
for(int i=1;i<=n;i++)
{
m[i]=M/a[i];
ll y=mul(m[i],inv(m[i],a[i]),M);
x=(x+mul(y,b[i],M))%M;
}
write(x,'\n');
return ot(),0;
}