Gym
(Gym.cpp/c/pas)
Topic Description
Muji finally arrives at the gym, VAN 様's lair, but he is already alone. He decided to have a showdown with VAN. He decided to play ♂run♂step with VAN . Known track length $ l \(m, and Muji can run one step and can only run\) n \(m, VAN can run one step and can only run\) m \(meter. Now it is stipulated that the runner cannot run out\) k \(m. And whoever runs farther in the end wins. To be fair, \) k$ is a completely random positive integer between $1 \( to \) l $. Now Muji wants to know what is the probability of a draw with VAN 様.
Input Description (gym.in) Input Description
The first line is three integers, followed by \(l,n,m\) ;
Output Description (gym.out) Output Description
A reduced true score, in the format a/b, which is the probability of a draw between Muji and VAN
Sample Input
10 3 2
Sample Output
3/10
Sample Interpretation
When $k$ is $1,6,7$, Muji will tie with VAN 様
Data Range Data Size
For 30% of the data, \(n,m,l\le 10^6\)
For 100% data, \(n,m,l\le 5\times {10}^{18}\)
answer
First determine that this is a number thesis, so I think in this direction.
Obviously, the necessary and sufficient conditions for a draw between Muji and VAN are: \(k\mod n=k\mod m\) .
It is not difficult to find that when \(n=m\) , \(k\) obviously holds. The above formula will report an error, so you need to make a special judgment:
if(n==m)
{
fout<<"1/1";
return 0;
}
Then go on to start the happy derivation...
Convert the \(k\) in the above formula into the formula with remainder, there are \(k-t_1 n=k-t_2 m\) , \(t_1 n=t_2 m\) .
Let the total number of \(k\) satisfying the tie between Muji and VAN be \(ans\) .
It is not difficult to find that when \(k=\infty\) , Muji and VAN meet for the first time at \([a,b]\) . So, when \(k<[a,b]\) , \(ans=\min(m,n)\) (neither of them took a step).
Following this line of thinking, we find that two people are equivalent at \([a,b]\) at the starting point. So it is not difficult for us to come up with a correct solution.
The cancer is that \([a,b]\) unsigned long long can't be stored... So long double must be used if necessary .
code
#include <fstream>
#include <algorithm>
using namespace std;
typedef long long LL;
LL l,n,m;
int main()
{
ifstream fin("gym.in");
ofstream fout("gym.out");
fin>>l>>n>>m;
if(n==m)
{
fout<<"1/1";
fin.close();
fout.close();
return 0;
}
LL t=min(n,m);
LL ans=t-1;
if(ans>l)
{
fout<<"1/1";
fin.close();
fout.close();
return 0;
}
LL g=__gcd(m,n);
LL lcm=m/g*n;
if((long double)m/g*n<=(long double)l)
ans+=l/lcm*t;
LL tmp=__gcd(ans,l);
fout<<ans/tmp<<'/'<<l/tmp;
fin.close();
fout.close();
return 0;
}