(a+b)%n = ((a%n)+(b%n))%n(a-b)%n = ((a%n)-(b%n)+n)%nab%n = (long long)(a%n)*(b%n)%n其中要注意a%n*b%n的值可能超int10-1 大整數模數輸入正整數n和m,輸出n mod m的值。n<=10^100,m<=10^9將大整數寫成“自左向右”的形式:1234%n=(((1*10+2)*10+3)*10+4)%n,然後就可以用之前的公式來處理了#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<map>#include<queue>#include<stack>#include<vector>#include<ctype.h>#include<algorithm>#include<string>#define PI acos(-1.0)using namespace std;int main (){ char n[110]; int m; scanf("%s%d",n,&m); int len=strlen(n); long long ans=0; for (int i=0; i<len; i++) ans=(ans*10+n[i]-'0')%m; printf("%d\n",ans); return 0;}10-2 冪模數輸入正整數a、n和m,輸出a^n mod m的值。a,n,m <= 10^9#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<map>#include<queue>#include<stack>#include<vector>#include<ctype.h>#include<algorithm>#include<string>#define PI acos(-1.0)using namespace std;int pow_mod(int a, long long n, int m){ if (n==0) return 1; int x=pow_mod(a,n/2,m); long long ans=(long long)x*x%m; if (n%2) ans=ans*a%m; return (int)ans;}int main (){ int a,m; long long n; cin>>a>>n>>m; int ans=pow_mod(a,n,m); cout<<ans<<endl; return 0;}10-3 模線性方程輸入正整數a,b,n,解方程ax≡b(mod n)。a,b,n<=10^9≡代表同餘。即ax-b=ny。不難看出這就是一個擴充歐幾裡得演算法還有就是ax≡1(mod n)的解稱為a關於模n的逆元。ax-ny=1, 這代表了gcd(a,n)=1,才有唯一解。否則無解