韩信点兵的小故事
3人报数 五人报数 七人报数
各余a b c,求军队可能最少人数
物不知其数问题出自一千六百年前我国古代数学名著《孙子算经》.原题为:”今有物不知其数,三三数之二,五五数之三,七七数之二,问物几何?”
例如我国明朝数学家程大位在他著的《算法统宗》(1593年)中就用四句很通俗的口诀暗示了此题的解法:
三人同行七十稀,
五树梅花甘一枝,
七子团圆正半月,
除百零五便得知.
(d=70×a+21×b+15×c >105)mod105;
最后的 d 为所求值。
70 21 15 怎么来的呢 》? 如下 中国剩余定理
![](https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D223/sign=afd164d67a310a55c024d9f684444387/7af40ad162d9f2d30fcbdacaaaec8a136327cc39.jpg)
![](https://gss3.bdstatic.com/7Po3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D203/sign=56bcfe3fc55c1038207ec9c28110931c/91ef76c6a7efce1b22fa36efac51f3deb58f65c6.jpg)
设
为
模
的数论倒数(
为
模
意义下的逆元)
的通解形式为
只有一个解:
例如:试求一数,使之用4除余m,用5除余n,用7除余k ,
![](https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D59/sign=98cf0fe8ff039245a5b5e10687947d4e/562c11dfa9ec8a133826aec5f403918fa0ecc0d3.jpg)
![](https://gss2.bdstatic.com/-fo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D20/sign=3421af3261d0f703e2b292dc08fa9d75/91ef76c6a7efce1b232431efac51f3deb48f658c.jpg)
![](https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D17/sign=9757be9e4410b912bbc1f2f9c3fd6213/f3d3572c11dfa9ecd055af3261d0f703918fc198.jpg)
![](https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D14/sign=600e2894abcc7cd9fe2d30dd3801d19f/dcc451da81cb39db7a5c542fd8160924ab18302e.jpg)
![](https://gss2.bdstatic.com/-fo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D20/sign=987bd370be003af349badb60342ace9c/d0c8a786c9177f3ec9a1cf7478cf3bc79f3d5635.jpg)
![](https://gss2.bdstatic.com/-fo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D17/sign=41ce7c820246f21fcd345a54f624e601/dc54564e9258d10925da9bcbd958ccbf6c814d89.jpg)
![](https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D270/sign=1e2b1996a1ec8a13101a50e7c7039157/5ab5c9ea15ce36d39c3ebe4932f33a87e950b194.jpg)
方程组
![](https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D20/sign=b30da9ccf1d3572c62e29bdc8b1383a3/9a504fc2d56285355c012c0493ef76c6a7ef6336.jpg)
![](https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D486/sign=cb3ee938d72a283447a637036db5c92e/2fdda3cc7cd98d10aa8ef514223fb80e7bec90b9.jpg)
在模
的意义下,方程组
![](https://gss1.bdstatic.com/-vo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D16/sign=e44c04e9c0cec3fd8f3ea373d6887f24/a8ec8a13632762d0aa5d5d9aa3ec08fa513dc6b0.jpg)
![](https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D20/sign=b30da9ccf1d3572c62e29bdc8b1383a3/9a504fc2d56285355c012c0493ef76c6a7ef6336.jpg)
![](https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D162/sign=6e46f98494510fb37c197391eb32c893/8ad4b31c8701a18b0f5da572942f07082838fe5d.jpg)
x= a*2*35+b*1*21+c*1*15
由以上方法可得 (105m+56n+120k )mod 140
( 一组值m,n,k 3 2 5 ,该数为 ((105*3+ 56*2+120*5)mod 140 = 47 .)
剩余定理给出的通解形式关键是求出 ti 即(Mi 模 mi 意义下的逆元)
so? 问题又转到了求逆元
欧几里得求逆元 https://www.cnblogs.com/cenariusxz/p/4323872.html
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #define ll long long void gcd(ll a,ll b,ll& d,ll& x,ll& y){ if(!b){d=a;x=1;y=0;} else {gcd(b,a%b,d,y,x);y-=x*(a/b);} } int main(){ ll a,b,d,x,y; while(scanf("%lld%lld",&a,&b)!=EOF){ gcd(a,b,d,x,y); printf("%lld*%lld+%lld*%lld=%lld ",a,x,b,y,d); } return 0; }
欧几里得 快速幂 费马小定理求逆元
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; typedef long long ll; ll extendGcd(ll a,ll b,ll &x,ll &y){ ll ans,t; if(b==0){ x=1;y=0; return a; } ans=extendGcd(b,a%b,x,y); t=x;x=y;y=t-(a/b)*y; return ans; } //返回x,a*x=1(mod m) ll getInv(ll a,ll m){ ll x,y,d; d=extendGcd(a,m,x,y); if(d==1) return (x%m+m)%m; else return -1; } long long euler(long long x) { long long res=x; for(long long i=2;i<(int)sqrt(x*1.0)+1;i++) if(x%i==0) { res=res/i*(i-1); while(x%i==0) x/=i; } if(x>1) res=res/x*(x-1); return res; } //如果m是1000000007这样的素数用费小求逆元 long long quickpowmod(long long x, long long y, long long mod) { long long ret = 1; while(y){ if(y&1) ret = ret*x%mod; x = x*x%mod; y >>= 1; } return ret; } int main() { ll a,m; while(cin>>a>>m){ cout<<getInv(a,m)<<endl; //m不是素数 cout<<quickpowmod(a,m-2,m)<<endl; //m是素数 } return 0; } --------------------- 本文来自 奶瓶他哥 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/naipp/article/details/51880008?utm_source=copy