POJ 2132 Cow Math
来源:互联网 发布:win7红警2点网络进不去 编辑:程序博客网 时间:2024/05/16 06:26
此题我用的DFS过。
DFS每一条路,用res=gcd(res,now),不断更新res值。当访问到2时,用ans=lcm(ans,res),更新答案,即可。
剪枝:如果当前的ans可以整除当前的res,就剪掉,因为当ans%res=0,必然有lcm(ans,res)=ans。
剩余一个疑问就是:为什么lcm(a,b,c)=lcm(lcm(a,b),c)?
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int n,a[30][30],v[30];long long ans;long long gcd(long long x,long long y){ long long t,r; if(x<y) { t=x; x=y; y=t; } while(y) { r=x%y; x=y; y=r; } return x;}long long lcm(long long x,long long y){ return x*y/gcd(x,y);}void DFS(int t,long long res){ int i; if (t == 2) { ans=lcm(ans,res); return ; } if (res != -1 && ans%res == 0) return ; for (i=1; i<=n; i++) { if (v[i] == 0 && a[t][i] != 0) { v[i]=1; if (res != -1) DFS(i,gcd(res,a[t][i])); else DFS(i,a[t][i]); v[i]=0; } }}int main(){ int i,j; scanf("%d",&n); for (i=1; i<=n; i++) { for (j=1; j<=n; j++) { scanf("%d",&a[i][j]); } v[i]=0; } ans=1; v[1]=1; DFS(1,-1); printf("%lld\n",ans);}
- POJ 2132 Cow Math
- POJ 2132 Cow Math DP
- POJ 1946 Cow Cycling
- POJ 3176 COW BOWLING
- poj 1946 Cow Cycling
- POJ 3615 Cow Hurdles
- POJ 3176 Cow Bowling
- poj 3615 Cow Hurdles
- poj 3176 Cow Bowling
- POJ 3176 Cow Bowling
- poj 3660 Cow Contest
- POJ 1946 Cow Cycling
- POJ 1985 COW MARATHON
- poj 3270 Cow Sorting
- Poj 3176 Cow Bowling
- POJ 3176 Cow Bowling
- POJ 3660 Cow Contest
- poj 3176 Cow Bowling
- Cocoa 图形开发基础
- Ubuntu下安装google-chrome
- 打开网络设置和电话拨号界面
- 【解惑】深入jar包:从jar包中读取资源文件
- js性能优化
- POJ 2132 Cow Math
- Android之MVC模式
- Jquery 取值
- strace 查看加载动态库的情况
- 历年北美票房排行榜
- sublime text 2 快捷键
- PAT-1040
- 有效控制Android应用程序的耗电量
- Android-自定义TextView和异步加载图片的实现