您的位置:首页 > Web前端

hihoCoder [Offer收割]编程练习赛8 小Ho的强迫症 (裴蜀定理)

2017-03-05 17:53 471 查看
题目原文:http://hihocoder.com/problemset/problem/1473#

描述

小Ho在一条笔直的街道上散步。街道上铺着长度为L的石板,所以每隔L距离就有一条石板连接的缝隙,如下图所示。



小Ho在散步的时候有奇怪的强迫症,他不希望脚踩在石板的缝隙上。(如果小Ho一只脚的脚尖和脚跟分别处于一条缝隙的两侧,我们就认为他踩在了缝隙上。如果只有脚尖或脚跟接触缝隙,不算做踩在缝隙上)  

现在我们已知小Ho两只脚的长度F以及每一步步伐的长度D。如果小Ho可以任意选择起始位置,请你判断小Ho能否保持不踩缝隙散步至无穷远处?

输入

第一行是一个整数T,表示测试数据的组数。

每组测试数据包含3和整数L, F和D,含义如上文所述。

对于30%的数据: L <= 1000  

对于60%的数据: L <= 100000

对于100%的数据: L <= 100000000, F <= 100000000, D <= 100000000, T <= 20

输出

对于每组数据输出一行YES或者NO,表示小Ho是否能走到无穷远处。

解题思路:一眼看上去没什么想法,所以将题目转化为数学模型,也就是存在任意一个起点 x

,将它左侧最近的一条线设为0点,那么问题转化为 

X + k * D <= m * L //走k步后脚后跟在前面那条线后

X + k *D + F <= m*L //走k步后脚尖在前面那条线后

如果想要永远碰不到线,则需要对任意的 K 都成立。

将上面的式子转化一下就变为了

X  <= m * L -[b]
k * D
[/b]

X
+ F  <= m * L -[b] k * D
[/b]

[b]因为X
>= 0
[/b]

所以一定要满足
F <= m*L- k*D  此时由裴蜀定理可以知道 [b]m*L- k*D
的最小正值为 gcd (L , D)[/b]

[b]所以当
F <= gcd (L , D)时永远不会碰到线
[/b]



[b]AC代码:[/b]



/*
@Author: wchhlbt
@Date:   2017/3/5
*/
//#include <bits/stdc++.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <limits>
#include <climits>
#include <cstdio>

#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 1000005
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;

const double eps =1e-8;
const int mod = 1000000007;
const double PI = acos(-1.0);

int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

int gcd(int a, int b)
{
if(a%b==0)  return b;
return gcd(b,a%b);
}
int main()
{
//cin>>n;
//ios_base::sync_with_stdio(false);
//cin.tie(0);
//cout << fixed << setprecision(11);

int t;
cin>>t;
while(t--)
{
int l,d,f;
cin>>l>>f>>d;
//cout << gcd(l,d) << endl;
if(gcd(l,d)>=f)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: