B. 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.
这题很有意思。。把那些写出来就是。。。假设qi是所有质数
ans = (q2-q1)/q1q2 + (q3-q2)/q2q3 + ....
而这个(q2-q1)/q1q2 + (q3-q2)/q2q3 = (q3-q1)/q1q3....
然后,还要我说吗?
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<queue>
#include<string>
#define LL long long
using namespace std;
const int isOJ = 1,Debug = 0;
bool isPrime(LL k){
LL i;
for (i = 2;i*i <= k;i++)
if (k % i == 0)
return 0;
return 1;
}
LL gcd(LL a,LL b){
if (b == 0)
return a;
else
return gcd(b,a%b);
}
int cas;
int Work(){
LL n,minp,maxp,a1,b1,a2,b2,a3,b3,t;
cin >> n;
minp = n;maxp = n+1;
while(!isPrime(minp)) minp--;
while(!isPrime(maxp)) maxp++;
a1 = minp - 2;
b1 = minp * 2;
t = gcd(a1,b1);
a1 /= t;
b1 /= t;
a2 = n - minp + 1;
b2 = maxp * minp;
t = gcd(a2,b2);
a2 /= t;
b2 /= t;
t = gcd(b1,b2);
a3 = a1 * (b2/t) + a2 * (b1/t);
b3 = b1 / t * b2 ;
//cout << a3 << "fuck" << b3 << endl;
t = gcd(a3,b3);
a3 /= t;
b3 /= t;
cout << a3 << "/" << b3 << endl;
return 1;
}
int main(){
if (!isOJ)
freopen("/Users/mac/Desktop/test.in","r",stdin);
cin >> cas;
while(cas--) Work();
return 0;
}