code chef - Cool Guys题解

本文详细介绍了如何通过编程解决问题:给定一个整数N,随机选择两个整数A和B(1到N之间),计算A和B的最大公约数等于B的概率。通过求解最大公约数的算法和数学公式,本文提供了完整的解决方案和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

All submissions for this problem are available.

Given an integerN. IntegersAandBare chosen randomly in the range[1..N]. Calculate the probability that the Greatest Common Divisor(GCD) ofAandBequals toB.

Input

The first line of the input contains an integerTdenoting the number of test cases. The description ofTtest cases follows. Each test case consists of a single integerNon a separate line.

Output

For each test case, output a single line containing probability as an irreducible fraction.

Given an integerN. IntegersAandBare chosen randomly in the range[1..N]. Calculate the probability that the Greatest Common Divisor(GCD) ofAandBequals toB.

Input

The first line of the input contains an integerTdenoting the number of test cases. The description ofTtest cases follows. Each test case consists of a single integerNon a separate line.

Output

For each test case, output a single line containing probability as an irreducible fraction.

Example

Input:
3
1
2
3

Output:
1/1
3/4
5/9

Constraints

1<=T<=103

1<=N<=109


本题也是个数学题。

有两个知识点:

1 求最大公约数的算法

2 求小于等于一个数值的两数相乘的对数 - 这个又是数学公式


据说这里是数学详细解析,感兴趣的研究一下:

http://matwbn.icm.edu.pl/ksiazki/mon/mon42/mon4204.pdf

OJ系统:

http://www.codechef.com/problems/COOLGUYS/

long long mGcd(long long a, long long b)
{
	long long c = 0;
	while (b)
	{
		c = b;
		b = a % b;
		a = c;
	}
	return c;
}

long long pairsOfCoolguys(long long n)
{
	long long ans = 0;
	long long sq = (long long)sqrt(n);
	for (unsigned i = 1; i <= sq; i++)
	{
		ans += n/i;
	}
	ans = (ans<<1) - sq*sq;
	return ans;
}

void coolguys()
{
	long long n = 0;
	int T = 0;
	cin>>T;
	while (T--)
	{
		cin>>n;
		long long ps = pairsOfCoolguys(n);
		n *= n;
		long long d = mGcd(ps, n);
		cout <<(ps/d) << "/"<<(n/d)<<"\n";
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值