【HDU
来源:互联网 发布:java 系统时间 格式化 编辑:程序博客网 时间:2024/05/22 07:43
这道题是为了记录一下这个差点被忘记,但是十分重要的数学结论:
gcd(X,Y) = gcd(X, Y - K*X) = gcd(X - K * Y, Y)
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;//gcd(x, y) = gcd(x, y - k *x) = gcd(x - k * y, y)//其实这个就是欧几里得算法求GCD的主要思想typedef long long ll;ll gcd(ll n, ll m){ if(n % m == 0) return m; return gcd(m, n % m);}int main(){ int CASE; scanf("%d", &CASE); for(int cas = 1; cas <= CASE; ++cas) { ll x, y; scanf("%lld%lld", &x, &y); int ans = 1; while(x != y) { if(x > y) swap(x, y); ll pre = y; ll g = gcd(x, y); y = y / (1 + x / g); if(y + x * y / gcd(x, y) != pre) break; ans += 1; } printf("Case #%d: %d\n", cas, ans); } return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 浅谈svn版本冲突的解决方法
- 文件的写入
- Android OpenGL ES 绘图基本概念
- 关于IP地址与整型之间的转换
- java实现(RSA非对称加密) SHA1WithRSA加签验签 及openssl生成公私钥
- 【HDU
- salesforce中RemoteObjects的应用
- 岭回归、LASSO与elastic net
- Lucene 全文检索技术
- innerText和innerHTML的区别
- 基于Redis实现分布式锁,Redisson使用及源码分析
- Google Summer of Code 2017 Summary
- shell of zip like war
- Android 数据库框架ormlite 使用精要