您的位置:首页 > 其它

洛谷 P1290 欧几里德的游戏 黄金分割

2016-10-21 20:00 375 查看
洛谷 P1290 欧几里德的游戏

解法一:

懒得写证明了, 谢谢帝江……… 可以参考解法2;

//解法1博客:http://blog.csdn.net/u013598409/article/details/49823643

#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
int main()
{
int T;
cin >> T;
int m, n;
while(T --)
{
cin >> m >> n;
if(n > m) swap(n,m);
int k = 0;
while(n > 0)
{
int t = m%n;
if(!t || m>=2*n) break;
m = n; n = t;
k ++;
}
if(k%2 == 0) puts("Stan wins");
else puts("Ollie wins");
}
return 0;
}


解法二:

(可以被卡精度)

感觉智商被碾压……

不严谨的证明:

设 G 为黄金分割比 0.6180……

假设 a < b 则, 若 a/b <= G, 则先手赢, 反之, 后手赢。

Fib: 1/2 == 0.5 ; 2/3 == 0.67 ; 3/5 == 0.6; 5/8 == 0.625 ……

可以发现 严格在 0.618 上下浮动。

显然, 谁的回合内先出现 1 谁赢。 2/3 == 0.67 > 0.618 的时候, 显然后手赢。

相等的时候,显然先手赢。

证明好随意……有错误欢迎指出(虚……

代码里用的黄金比是 1.618:

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;

int main()
{
LL T;
cin >> T;
double G = (sqrt(5)+1)/2;
while(T --)
{
LL a, b;
scanf("%lld%lld", &a, &b);
if(a < b) swap(a,b);
double c = (double)a/(double)b;
if(a == b || c >= G)    puts("Stan wins");
else puts("Ollie wins");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  游戏