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

题目原文: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 - k * D

X + F  <= m * L - k * D

因为X >= 0

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

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


AC代码:

/*
    @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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值