cf(思维题,判断两个大数的大小的比较方法)
2015-11-03 21:09
375 查看
C. The Big Race
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Vector Willman and Array Bolt are the two most famous athletes of Byteforces. They are going to compete in a race with a distance of Lmeters
today.
Willman and Bolt have exactly the same speed, so when they compete the result is always a tie. That is a problem for the organizers because they want a winner.
While watching previous races the organizers have noticed that Willman can perform only steps of length equal to w meters,
and Bolt can perform only steps of length equal to b meters. Organizers
decided to slightly change the rules of the race. Now, at the end of the racetrack there will be an abyss, and the winner will be declared the athlete, who manages to run farther from the starting point of the the racetrack (which is not the subject to change
by any of the athletes).
Note that none of the athletes can run infinitely far, as they both will at some moment of time face the point, such that only one step further will cause them to fall in the abyss. In other words, the athlete will
not fall into the abyss if the total length of all his steps will be less or equal to the chosen distance L.
Since the organizers are very fair, the are going to set the length of the racetrack as an integer chosen randomly and uniformly in range from 1 to t (both
are included). What is the probability that Willman and Bolt tie again today?
Input
The first line of the input contains three integers t, w and b (1 ≤ t, w, b ≤ 5·1018)
— the maximum possible length of the racetrack, the length of Willman's steps and the length of Bolt's steps respectively.
Output
Print the answer to the problem as an irreducible fraction
.
Follow the format of the samples output.
The fraction
(p and q are
integers, and both p ≥ 0 and q > 0 holds)
is called irreducible, if there is no such integer d > 1, that both pand q are
divisible by d.
Sample test(s)
input
output
input
output
Note
In the first sample Willman and Bolt will tie in case 1, 6 or 7 are
chosen as the length of the racetrack.
题意是
终点可以在1-t里面随便选择一个
终点之后都是陷阱,然后有两个人在比赛,一个人一步走w米,一个人一步走b米,谁能不越过终点的情况,走的最远,就算谁赢
然后问你选择平等的概率是多少
思路其实都有,只不过这题题目数据卡的死死的,到了10的18次方,所以如果w和b的最小公倍数越界了根本无法判断这个最小公倍数与 t 的关系,所以我们这里借助log()函数来搞定,强制转化成double之后一个log就可以轻松算出来俩数大小了。这个思路硬是没转过弯来,,,看了半天别人代码才看到的、、、、
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Vector Willman and Array Bolt are the two most famous athletes of Byteforces. They are going to compete in a race with a distance of Lmeters
today.
Willman and Bolt have exactly the same speed, so when they compete the result is always a tie. That is a problem for the organizers because they want a winner.
While watching previous races the organizers have noticed that Willman can perform only steps of length equal to w meters,
and Bolt can perform only steps of length equal to b meters. Organizers
decided to slightly change the rules of the race. Now, at the end of the racetrack there will be an abyss, and the winner will be declared the athlete, who manages to run farther from the starting point of the the racetrack (which is not the subject to change
by any of the athletes).
Note that none of the athletes can run infinitely far, as they both will at some moment of time face the point, such that only one step further will cause them to fall in the abyss. In other words, the athlete will
not fall into the abyss if the total length of all his steps will be less or equal to the chosen distance L.
Since the organizers are very fair, the are going to set the length of the racetrack as an integer chosen randomly and uniformly in range from 1 to t (both
are included). What is the probability that Willman and Bolt tie again today?
Input
The first line of the input contains three integers t, w and b (1 ≤ t, w, b ≤ 5·1018)
— the maximum possible length of the racetrack, the length of Willman's steps and the length of Bolt's steps respectively.
Output
Print the answer to the problem as an irreducible fraction
.
Follow the format of the samples output.
The fraction
(p and q are
integers, and both p ≥ 0 and q > 0 holds)
is called irreducible, if there is no such integer d > 1, that both pand q are
divisible by d.
Sample test(s)
input
10 3 2
output
3/10
input
7 1 2
output
3/7
Note
In the first sample Willman and Bolt will tie in case 1, 6 or 7 are
chosen as the length of the racetrack.
题意是
终点可以在1-t里面随便选择一个
终点之后都是陷阱,然后有两个人在比赛,一个人一步走w米,一个人一步走b米,谁能不越过终点的情况,走的最远,就算谁赢
然后问你选择平等的概率是多少
思路其实都有,只不过这题题目数据卡的死死的,到了10的18次方,所以如果w和b的最小公倍数越界了根本无法判断这个最小公倍数与 t 的关系,所以我们这里借助log()函数来搞定,强制转化成double之后一个log就可以轻松算出来俩数大小了。这个思路硬是没转过弯来,,,看了半天别人代码才看到的、、、、
#include <iostream> #include <stdio.h> #include<stdlib.h> #include <math.h> #include<algorithm> using namespace std; typedef long long ll; ll gcd(ll a,ll b) { return b? gcd(b,a%b):a; } ll lcm(ll a,ll b) { return a*(b/gcd(a,b)); } int main() { ll t,w,b; cin>>t>>w>>b; if(w==b) cout<<"1/1"<<endl; else if(log(w*1.0/gcd(w,b)*(double)b)>log((double)t)) { ll s=min(t,min(w-1,b-1)),x=gcd(s,t); cout<<s/x<<'/'<<t/x<<endl; } else { ll min0=min(w,b)-1,gg=lcm(w,b),s=min0; //cout<<"s:"<<s<<endl; //cout<<"lcm:"<<gg<<endl; ll x=t/gg; //cout<<"t/lcm:"<<x<<endl; //cout<<"s:"<<s<<endl; s+=(x-1)*(min0+1); s+=min(t-x*gg+1,min0+1); //cout<<"t-x*gg:"<<t-x*gg<<endl; gg=gcd(s,t); cout<<s/gg<<'/'<<t/gg<<endl; } return 0; }
相关文章推荐
- leetcode-Minimum Window Substring
- 使用系统类NSURLSessionDownloadTask实现断点续传(音乐视频下载等)
- C/C++基本数据类型大小及表示范围
- SUMO使用教程(五)
- .NET中值得体验的精妙设计
- LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)
- MagicalRecord的使用(入门级别)
- CUDA-GDB调试一般方法————矩阵乘的例子
- windows下安装lavavel5
- nyoj57--6174问题
- EF在转换成JsonResult时遇到无限循环的解决办法
- IOS关闭键盘以及接受键盘打开关闭事件
- java中求一个字符创的最大不重复长度
- git学习三:git常用命令
- 判断节点为元素节点 HTML XML
- SUMO使用教程(四)
- Memcached 缓存个体,对象,泛型,表
- 【UKIEPC2015 H】【凸包思维栈操作】Sunlight 每座建筑的日照时间
- JS 制作九九乘法表
- Mapreduce与Hive比较