Codeforces 396B 数论

On Sum of Fractions
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's assume that

  • v(n) is the largest prime number, that does not exceed n;
  • u(n) is the smallest prime number strictly greater than n.

Find .

Input

The first line contains integer t (1 ≤ t ≤ 500) — the number of testscases.

Each of the following t lines of the input contains integer n (2 ≤ n ≤ 109).

Output

Print t lines: the i-th of them must contain the answer to the i-th test as an irreducible fraction "p/q", where p, q are integers, q > 0.

Examples
input
2
2
3
output
1/6
7/30



题意:定义v(n)是不超过n的最大素数, u(n)是大于n的最小素数。
以分数形式"p/q"输出 sigma(i = 2 to n) (1 / (v(i)*u(i))), pq为互质整数且q > 0。


题解:可以把素数看成端点

那么问题就变成了区间求和

假设一段区间的左边是p1  右边是p2

那么这段区间的和就是(p2-p1)/(p2*p1)=1/p1-1/p2

所以答案就是  1/p1-1/p2+1/p2-1/p3....+1/(p(k-1))-1/pk+(n-p(k+1))/(pk*p(k+1))

1/2-1/pk+(n-pk+1)/(pk*(pk+1))
也就是(pk*p(k+1)-2*p(k+1)+2*(n-pk+1))/(2*pk*p(k+1))

再求个gcd就行了


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
bool check(ll t){
	for(ll i=2;i*i<=t;i++){
		if(t%i==0)return 0;
	}
	return 1;
}
int main(){
	ll t,n;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld",&n);
		ll a,b,i;
		for(i=n;;i--){
			if(check(i)){
				a=i;
				break;
			}
		}
		for(i=n+1;;i++){
			if(check(i)){
				b=i;
				break;
			}
		}
		ll k=__gcd(a*b-2*b+2*(n-a+1),2*a*b);
		printf("%lld/%lld\n",(a*b-2*b+2*(n-a+1))/k,2*a*b/k);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值