【模板】CRT/EXCRT 中国剩余定理/扩展中国剩余定理
problem
求这个关于
中国剩余定理 CRT
适用范围:
做法:令
答案是
证明:当
点击查看代码
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr,##__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
typedef long long LL;
LL mod(LL x,LL m){return(x%m+m)%m;}
LL exgcd(LL a,LL b,LL c,LL& x,LL& y){
if(!b) return x=c/a,y=0,a;
LL res=exgcd(b,a%b,c,y,x);
return y-=a/b*x,res;
}
LL solve(LL a,LL b,LL c){
LL x,y,d=exgcd(a,b,c,x,y);
return c%d==0?mod(x,b/d):-1;
}
int n,a[20],b[20];
LL M=1,ans;
int main(){
// #ifdef LOCAL
// freopen("input.in","r",stdin);
// #endif
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]),M*=a[i];
for(int i=1;i<=n;i++){
LL mi=M/a[i];
ans+=1ll*b[i]*mi*solve(mi,a[i],1);
}
printf("%lld\n",ans%M?ans%M:M);
return 0;
}
扩展中国剩余定理 EXCRT
和 CRT 一点关系没有。
考虑暴力。现在考虑的方程组的
加入一个新的方程组,我们不断地使得
考虑优化,设要加
等式两边有一些系数可以对
点击查看代码
模板题很有可能爆 long long。我不想改了。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL mod(LL x,LL m){return(x%m+m)%m;}
LL exgcd(LL a,LL b,LL c,LL& x,LL& y){
if(!b) return x=c/a,y=0,a;
LL res=exgcd(b,a%b,c,y,x);
y-=a/b*x;
return res;
}
LL solve(LL a,LL b,LL c,LL x=0,LL y=0){
return c%exgcd(a,b,c,x,y)==0?mod(x,b):-1;
}
LL lcm(LL a,LL b){
LL x,y;
return a/exgcd(a,b,1,x,y)*b;
}
LL n,ans,tot=1;
bool update(LL a,LL b){
// while(ans%a!=b) ans+=tot;
// ans+kt=b(mod a)=>tk+ay=b-ans
LL z=solve(mod(tot,a),a,mod(b-ans,a));
if(z==-1) return 0;
ans+=z*tot,tot=lcm(tot,a),ans=mod(ans,tot);
return 1;
}
int mian(){
bool flag=0;
for(int i=1;i<=n;i++){LL a,b;
scanf("%lld%lld",&a,&b);
if(!update(a,b)) flag=1;
}
long long x=ans;
printf("%lld\n",x);
return 0;
}
void reset(){
ans=0,tot=1;
}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
while(~scanf("%d",&n)) reset(),mian();
return 0;
}
傻掉了,还有 haskell 代码
点击查看代码
-- Prelude.gcd
-- gcd :: Integral a => a -> a -> a
-- gcd x 0 = x
-- gcd x y = gcd y (x `mod` y)
-- Prelude.lcm
-- lcm :: Integral a => a -> a -> a
-- lcm x y = x * (y `div` (gcd x y))
exgcd :: Integral a => (a, a) -> (a, a)
exgcd (a, 0) = (1, 0)
exgcd (a, b) = let (x, y) = exgcd (b, a `mod` b) in (y, x - (a `div` b) * y)
merge :: Integral a => (a, a) -> (a, a) -> (a, a)
-- Promise: there is at least one vaild solution
-- or check it by (r1 - r2) `mod` (gcd m1 m2) == 0
merge (r1, m1) (r2, m2) = (r3 `mod` l, l) where
d = gcd m1 m2
l = lcm m1 m2
(x, y) = exgcd (m1 `div` d, m2 `div` d)
k = (r2 - r1) `div` d
r3 = r1 + k * x * m1
reduce :: (a -> a -> a) -> [a] -> a
reduce f (x:xs) = foldl f x xs
readIntListLn :: IO [Int]
readIntListLn = fmap (map read . words) getLine
readIntegerListLn :: IO [Integer]
readIntegerListLn = fmap (map read . words) getLine
main :: IO ()
main = do
[n] <- readIntListLn
input <- sequence $ replicate n readIntegerListLn
let (r, m) = reduce merge $ map (\[x, y] -> (y, x)) input
print r
-- sequence :: (Traversable t, Monad m) => t (m a) -> m (t a)
-- replicate :: Int -> a -> [a]
本文来自博客园,作者:caijianhong,转载请注明原文链接:https://www.cnblogs.com/caijianhong/p/template-crt.html
分类:
template
标签:
maths
, number theory
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!