CF#328 (Div. 2) C(大数)
1 second
256 megabytes
standard input
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 L meters 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 onlysteps of length equal to w meters, and Bolt can perform only steps of length equal to bmeters. 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?
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.
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 p and q are divisible by d.
10 3 2
3/10
7 1 2
3/7
In the first sample Willman and Bolt will tie in case 1, 6 or 7 are chosen as the length of the racetrack.
题目看了半天啊!!!看懂了算法很好想,可是要用到大数,电脑没装eclipse,gvim写java也是醉醉的,还好最后过的还不算晚。贴一下代码留着以后参考,后面还要仔细整理下BigInteger,BigDecimal
/*
ID: LinKArftc
PROG: Main.java
LANG: JAVA
*/
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
BigInteger t, w, b;
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
t = input.nextBigInteger();
w = input.nextBigInteger();
b = input.nextBigInteger();
BigInteger mi = w.min(b);
BigInteger gcd = w.gcd(b);
BigInteger lcm = w.multiply(b).divide(gcd);
BigInteger ans, cnt, res;
if (lcm.compareTo(t) > 0) {
ans = t.min(w.subtract(BigInteger.ONE).min(b.subtract(BigInteger.ONE)));
} else {
cnt = t.divide(lcm);
res = t.subtract(cnt.multiply(lcm));
ans = cnt.multiply(w.min(b)).add(res.min((w.subtract(BigInteger.ONE)).min(b.subtract(BigInteger.ONE))));
}
gcd = ans.gcd(t);
System.out.println(ans.divide(gcd)+"/"+t.divide(gcd)); }
}
}
再贴一发Python3代码
import sys
import fractions
for line in sys.stdin:
li = line.split()
t = int(li[0])
w = int(li[1])
b = int(li[2])
mi = min(w, b)
Gcd = fractions.gcd(w, b)
Lcm = w * b // Gcd
if Lcm > t:
ans = min(t, w - 1, b - 1)
else:
cnt = t // Lcm
res = t - cnt * Lcm
ans = cnt * min(w, b) + min(res, w - 1, b - 1)
Gcd = fractions.gcd(ans, t)
print("%s/%s" % (ans // Gcd, t // Gcd))
CF#328 (Div. 2) C(大数)的更多相关文章
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
- CF #375 (Div. 2) D. bfs
1.CF #375 (Div. 2) D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...
- CF #374 (Div. 2) D. 贪心,优先队列或set
1.CF #374 (Div. 2) D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...
- CF #374 (Div. 2) C. Journey dp
1.CF #374 (Div. 2) C. Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...
- CF #371 (Div. 2) C、map标记
1.CF #371 (Div. 2) C. Sonya and Queries map应用,也可用trie 2.总结:一开始直接用数组遍历,果断T了一发 题意:t个数,奇变1,偶变0,然后与问的 ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组
题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)
转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...
- CF#138 div 1 A. Bracket Sequence
[#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...
- Codeforces Round #328 (Div. 2)
这场CF,准备充足,回寝室洗了澡,睡了一觉,可结果... 水 A - PawnChess 第一次忘记判断相等时A先走算A赢,hack掉.后来才知道自己的代码写错了(摔 for (int i=1; ...
随机推荐
- Java并发基础--多线程基础
一.多线程基础知识 1.进程和线程 进程:是指一个内存中运行的应用程序,每个进程都有一个独立的内存空间,一个应用程序可以同时运行多个进程:进程也是程序的一次执行过程,是系统运行程序的基本单位:系统运行 ...
- 02-Mysql数据库----初识
什么是数据(Data) 描述事物的符号记录称为数据,描述事物的符号既可以是数字,也可以是文字.图片,图像.声音.语言等,数据由多种表现形式,它们都可以经过数字化后存入计算机 在计算机中描述一个事物,就 ...
- [Linux] umount目录提示device is busy的解决方法
使用sshfs等方式挂载的目录出现问题时,使用umount卸载经常提示device is busy,如果仔细阅读错误提示就可以找到命令lsof和fuser命令. 其实原因就是有进程占用当前目录,导致不 ...
- bbbbbeta
about beta 写在开头:(小声bb,无任何专业知识) 好了正文开始了 = = beta冲刺对于来说可能是让我觉得非常有成就感的叭,相比于alpha,每天都能写代码的感觉真好鸭(认真脸)(虽然天 ...
- Linux C++线程池实例
想做一个多线程服务器测试程序,因此参考了github的一些实例,然后自己动手写了类似的代码来加深理解. 目前了解的线程池实现有2种思路: 第一种: 主进程创建一定数量的线程,并将其全部挂起,此时线程状 ...
- Ext.Net中如何获取组件
我们在编写函数function的时候,常常需要用到页面上的组件.这时候就需要调用组件. 在Ext.net中,调用组件可以用.App.ID.(ID指的是想要调用的组件的ID). 例如: 我写一个函数需要 ...
- ArcGIS Engine开发中利用GP工具时常出现的错误
在用GP工具的时候常常会碰到这个错误: 调用 GP 对 COM 组件的调用返回了错误 HRESULT E_FAIL 解决方案: 这种情况一般有两种可能. 1.AE程序的license的级别不够. 2. ...
- Java基础——集合
java的集合类是一个工具类,存放在java.util包中.它不仅可以存储对象,也可以实现常用数据结构,如栈.队列等.严格的说,集合类存放的是对象的引用,而不是对象本身. java集合主要由这两个接口 ...
- Java堆和栈
栈中存基本类型变量数据和对象的引用 堆中存new的对象
- 计蒜客16495 Truefriend(fwt)
#include <iostream> #include <cstring> #include <cstdio> using namespace std; type ...