hihoCoder [Offer收割]编程练习赛8 小Ho的强迫症 (裴蜀定理)
2017-03-05 17:53
471 查看
题目原文:http://hihocoder.com/problemset/problem/1473#
小Ho在散步的时候有奇怪的强迫症,他不希望脚踩在石板的缝隙上。(如果小Ho一只脚的脚尖和脚跟分别处于一条缝隙的两侧,我们就认为他踩在了缝隙上。如果只有脚尖或脚跟接触缝隙,不算做踩在缝隙上)
现在我们已知小Ho两只脚的长度F以及每一步步伐的长度D。如果小Ho可以任意选择起始位置,请你判断小Ho能否保持不踩缝隙散步至无穷远处?
每组测试数据包含3和整数L, F和D,含义如上文所述。
对于30%的数据: L <= 1000
对于60%的数据: L <= 100000
对于100%的数据: L <= 100000000, F <= 100000000, D <= 100000000, T <= 20
解题思路:一眼看上去没什么想法,所以将题目转化为数学模型,也就是存在任意一个起点 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]
描述
小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; }
相关文章推荐
- [Offer收割]编程练习赛8 A 小Ho的强迫症
- [Offer收割]编程练习赛1 hihocoder 1268 九宫 (DFS)
- hihoCoder[Offer收割]编程练习赛3题目解析
- [Offer收割]编程练习赛1 hihocoder 1269 优化延迟 (二分+优先权队列)
- HihoCoder1655 : 第K小最简真分数([Offer收割]编程练习赛39)(唯一分解+容斥定理+二分)(不错的数学题)
- 时间模板【hihocoder [Offer收割]编程练习赛9 A】【模拟】闰秒
- hihocoder [Offer收割]编程练习赛14 小Hi和小Ho的礼物
- HihoCoder1656 : 前缀后缀查询([Offer收割]编程练习赛39)(字典树+小技巧)
- 【hihocoder [Offer收割]编程练习赛9 B】【水题】水陆距离
- hihocoder 1569 [Offer收割]编程练习赛25 : 无限巧克力谜题
- [hihocoder][Offer收割]编程练习赛46
- Hihocoder [Offer收割]编程练习赛70 解题报告 By cellur925
- hihocoder [Offer收割]编程练习赛14 投掷硬币
- [Offer收割]编程练习赛1 hihocoder 1270 建造基地 (完全背包)
- [Offer收割]编程练习赛2 hihocoder 1275 扫地机器人 (计算几何+模拟 比较烦)
- 【hihocoder [Offer收割]编程练习赛9 C】【简单DP】三等分
- hihoCoder [Offer收割]编程练习赛50 C 末尾有最多0的乘积(动态规划)
- hihocoder[Offer收割]编程练习赛6及参考
- hihocoder [Offer收割]编程练习赛14 可疑的记录
- 【hihocoder [Offer收割]编程练习赛9 D】【简单DP】矩阵填数