跳到正文

个人记录

进阶算法计划【后期 / 夏秋 / 2023】TG13 数论笔记

littlebug约 16 分钟
原文
文章信息与版本

文章信息

发布
2023年8月9日
收录
2025年12月12日
更新
2026年11月7日
篇幅
约 16 分钟
获赞
1

版本

  1. v1当前

B3716 分解质因子 3

link

C++
#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";
	}
}

威尔逊定理

(p−1)!≡−1(modp)(p-1)! \equiv -1 \pmod p

欧拉函数

定义: φ(i)\varphi (i)表示小于i的正整数中与i互质的数的个数

i为质数时:

φ(i)=i−1\varphi (i) = i-1

线性筛求欧拉函数:

C++
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);
		}
	}
}

逆元

定义: x(modp)x \pmod p 的逆元满足

xx−1≡1(modp)xx^{-1} \equiv 1 \pmod p

定理: x(modp)x \pmod p 存在逆元当且仅当 gcd⁡(x,p)=1\gcd(x,p)=1

定理: 模 pp 同余下,整数 xx 的逆元若存在,则唯一

定理: 在模质数 pp 同余下,[1,p−1][1,p-1] 内所有整数的逆元互不相同

定理: 一个数的逆元的逆元等于它自身

exgcd求逆元(p可以不为质数):

C++
#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;
}

欧拉定理: 对于任意正整数 a,ma,m 且 gcd⁡(a,m)=1\gcd(a,m)=1 一定有

aφ(m)≡1(modm)a^{\varphi(m)} \equiv 1 \pmod m

当 mm 为质数时:

费马小定理: 对任意正整数 aa 和质数 pp ,

ap−2≡1(modp)a^{p-2} \equiv 1 \pmod p

P2613:

link

C++
#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;
}

定理 设 inviinv_i 表示 i i 的逆元,有递推式:

invi≡(p−⌊p/i⌋)×invpmod  i(modp)inv_i \equiv (p- \lfloor p/i \rfloor) \times inv_{p \mod i} \pmod p

B3717:

link

C++
#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;
}

中国剩余定理

定义:

给定 nn 个线性同余方程:

{x≡a1(modm1)x≡a2(modm2)…x≡an(modmn)\begin{cases} x \equiv a_1 \pmod {m_1} \\ x \equiv a_2 \pmod {m_2} \\ \dots \\ x \equiv a_n \pmod {m_n} \end{cases}

其中保证 m1,m2,…mnm_1,m_2, \dots m_n 两两互质,求 xx 的通解

记 M=∏miM=\prod m_i , Mi=M/miM_i=M/m_i , tit_i 是 MiM_i 在模 mim_i 群下的逆元,

则方程组的唯一通解为:

x≡∑i=1naitiMi(modM)x \equiv \sum_{i=1}^{n} a_i t_i M_i \pmod M

P1495:

link

C++
#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

C++
#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;
}

visitors\mathtt{visitors}

原文