1. 程式人生 > 實用技巧 >CodeForces 595C-Warrior and Archer(數論+python寫法)

CodeForces 595C-Warrior and Archer(數論+python寫法)

題目連結:https://codeforces.com/problemset/problem/592/C
CSDN食用連結:https://blog.csdn.net/qq_43906000/article/details/107831159

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 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·10^{18})\) — 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 \(\frac{p}{q}\). Follow the format of the samples output.

The fraction \(\frac{p}{q}\)( 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.

Examples
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到賽道長度中隨機選擇的,終點之後是一個深淵,每次給你他們一步跨出的距離,問他們存活下來後平局的概率是多少。

emmm,挺折騰人的這題。。實際上我們直接去找他們的最小公倍數就好了,然後在賽道\(n\)的長度下判斷有多少個最小公倍數,假設可以存在\(x\)個最小公倍數,那麼前\(x-1\)個最小公倍數之後一定會存在\(min(a,b)-1\)個數使得兩個人都無法到達,而還需要考慮的是\(0\)之後和\(x-1\)個之後,\(0\)之後的話也是一樣地加上\(min(a,b)-1\)個,\(x-1\)個的話就需要判斷\(lcm(a,b)*x+min(a,b)-1\)是否大於總長了。

用C++寫的話大概就是這樣的:(考慮到會爆long long,我們要使用__int128,將下面的程式碼中的ull替換為__int128再改下輸入和輸出就OK了)

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int mac=5e5+10;

int a[300][300];

int main(int argc, char const *argv[])
{
	ull t,a,b;
	cin>>t>>a>>b;
	if (a==b) {cout<<"1"<<"/"<<"1"<<endl; return 0;}
	if (a==1 || b==1){
		ull nb=t/max(a,b);
		if (a==b) cout<<"1"<<"/"<<"1"<<endl;
		else {
			ull p=__gcd(t,nb);
			cout<<nb/p<<"/"<<t/p<<endl;
		}
	}
	else {
		ull lcm=a/__gcd(a,b)*b;
		ull nb=t/lcm;
		ull fz=nb*min(a,b);
		if (t>=lcm*nb+min(a,b)) fz+=min(a,b)-1;
		else fz+=t-lcm*nb;
		ull p=__gcd(t,fz);
		cout<<fz/p<<"/"<<t/p<<endl;
	}
	return 0;
}

以下是python的AC程式碼:

s=input().split(' ')
t=int(s[0]); a=int(s[1]); b=int(s[2]);

def gcd(a,b):
    if b==0:
        return a
    return gcd(b,a%b)

if a==b:
    print("1/1")
    exit()

if a==1 | b==1:
    nb=max(a,b)
    p=gcd(t,nb)
    print(nb//p,end='')
    print("/",end='')
    print(t//p)

else :
    lcm=a//gcd(a,b)*b
    nb=t//lcm
    fz=nb*min(a,b)
    if t>=lcm*nb+min(a,b):
        fz=fz+min(a,b)-1
    else :
        fz=fz+(t-lcm*nb)
    p=gcd(t,fz)
    print(fz//p,end='')
    print("/",end='')
    print(t//p)