HDU1788 Chinese remainder theorem again
来源:互联网 发布:如何发淘宝店铺链接 编辑:程序博客网 时间:2024/06/06 02:00
题目大意:开始还真准备用同余线性方程组做了,后来仔细一看~~哟~~可以优化~~哈。
思路:因为
N%M1=M1-a
N%M2=M2-a
N%M3=M3-a
即:N%Mi=Mi-a,所以 N%Mi+a=Mi,所以N+a和0同余于Mi(i=1,2,3……),特别要记住:能够化成和0同余的一定要变式,因为这样简单多了,完全转化为求Mi的最小公倍数,
即是:N+a= (Mi的最小公倍数)
如此自然对于每个Mi都能够整除。
一开始,我是这样想的:
N=Mi-a+kMi的,准备用解线性同余方程的方式来做了。
AC program:#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>using namespace std; __int64 mm[12];__int64 gcd(__int64 a,__int64 b) { if(b==0)return a; return gcd(b,a%b); } int main(){int k,a;while(cin>>k>>a,k+a){ __int64 tmp=1; for(int i=0;i<k;i++) { cin>>mm[i]; if(tmp<mm[i]) //求N个数的最小公倍数是,用前面的最小公倍数当做一项来求后一项的最小公倍数, //同求N个数的最大公约数 swap(tmp,mm[i]); tmp=tmp*mm[i]/gcd(tmp,mm[i]); } cout<<tmp-a<<endl; } system("pause"); return 0;}
- HDU1788 Chinese remainder theorem again
- hdu1788 Chinese remainder theorem again
- HDU1788-Chinese remainder theorem again
- HDU1788 Chinese remainder theorem again 最小公倍数
- hdu1788 Chinese remainder theorem again 最小公倍数
- Chinese remainder theorem again(hdu1788中国剩余定理)
- HDU1788 Chinese remainder theorem again 中国剩余定理
- HDU1788 Chinese remainder theorem again【中国剩余定理】
- Chinese remainder theorem again + 数论
- hdu 1788 Chinese remainder theorem again
- Hdu 1788 Chinese remainder theorem again
- HDU 1788 Chinese remainder theorem again 数论
- hdu 1788 Chinese remainder theorem again
- HDU 1788 Chinese remainder theorem again
- hdu 1788 Chinese remainder theorem again(最小公倍数)
- 【数论】 HDOJ 1788 Chinese remainder theorem again
- Chinese remainder theorem again(HDU 1788)
- hdu 1788 Chinese remainder theorem again 最小公倍数
- Android对话框dialog大全
- 超实用的8个Linux命令行性能监测工具
- arm-linux-androideabi-gcc: Command not found
- 冒泡排序的改进
- Android Launcher icon 放大
- HDU1788 Chinese remainder theorem again
- HTML5特性 > Canvas >基础绘图
- linux虚拟文件系统浅析
- 字符编码详解及由来
- H2365 Override method should match case of ancestor的Hint提示说明
- apache2启动时共享库libpcre找不到
- ARM简介及BootLoader介绍
- c/c++ struct union 对齐方式
- 关于AutoResetEvent和ManualResetEvent 全面总结