题目链接:点击打开链接
时间限制: 5000ms
单点时限: 1000ms
内存限制: 256MB
小Hi最近在追求一名学数学的女生小Z。小Z其实是想拒绝他的,但是找不到好的说辞,于是提出了这样的要求:对于给定的两个正整数N和M,小Hi随机选取一个N的约数N',小Z随机选取一个M的约数M',如果N'和M'相等,她就答应小Hi。
小Z让小Hi去编写这个随机程序,到时候她review过没有问题了就可以抽签了。但是小Hi写着写着,却越来越觉得机会渺茫。那么问题来了,小Hi能够追到小Z的几率是多少呢?
每个输入文件仅包含单组测试数据。
每组测试数据的第一行为两个正整数N和M,意义如前文所述。
对于40%的数据,满足1<=N,M<=106
对于100%的数据,满足1<=N,M<=1012
对于每组测试数据,输出两个互质的正整数A和B(以A分之B表示小Hi能够追到小Z的几率)。
样例输入
3 2
样例输出
4 1
直接用map记录因子不知道会不会超时,据说map查找效率不是特别高,所以为了保险选用了set。
代码如下:
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
LL GCD(LL a,LL b)
{
return b == 0 ? a : GCD(b,a%b);
}
int main()
{
LL a,b;
set<LL> d;
scanf ("%lld %lld",&a,&b);
if (a == b) //特判
{
printf ("1 1\n");
return 0;
}
if (a > b)
swap(a,b);
LL t = sqrt(a);
LL ant1 = 0;
for (int i = 1 ; i <= t ; i++)
{
if (a % i == 0)
{
d.insert(i);
d.insert(a/i);
ant1 += 2;
}
}
if (t*t == a)
{
d.insert(t);
ant1--;
}
LL ant2 = 0;
LL com = 0;
t = sqrt(b);
for (int i = 1 ; i <= t ; i++)
{
if (b % i == 0)
{
ant2 += 2;
if (d.find(i) != d.end())
com++;
if (d.find(b/i) != d.end())
com++;
}
}
if (t*t == b && d.find(t) != d.end())
com--,ant2--;
LL up,down;
up = com;
down = ant1 * ant2;
LL g = GCD(up,down);
printf ("%lld %lld\n",down/g,up/g);
return 0;
}