Codeforces Round #232 (Div. 2) D(运算符重载)
2014-03-08 16:04
309 查看
D. On Sum of Fractions
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.
Sample test(s)
input
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.
Sample test(s)
input
2 2 3
output
1/6 7/30
typedef long long LL ; bool is_prime(LL x){ for(LL i = 2 ; i * i <= x ; i++) if(x % i == 0) return 0 ; return 1 ; } LL V(LL x){ while(! is_prime(x)) x-- ; return x ; } LL U(LL x){ x++ ; while(! is_prime(x)) x++ ; return x ; } LL gcd(LL x , LL y){ return y == 0 ? x : gcd(y , x % y) ; } class Node{ public : LL zi ; LL mu ; public : Node(){} ; Node(LL z , LL m){ LL g = gcd(z , m) ; zi = z/g ; mu = m/g ; } ; Node operator + (const Node &other){ LL m , z , g ; g = gcd(mu , other.mu) ; m = mu / g * other.mu ; z = other.mu / g * zi + mu /g * other.zi ; g = gcd(z, m) ; return Node(z/g , m/g) ; } Node operator - (const Node &other){ LL m , z , g ; g = gcd(mu , other.mu) ; m = mu / g * other.mu ; z = other.mu /g * zi - mu / g * other.zi ; g = gcd(z, m) ; return Node(z/g , m/g) ; } Node & operator = (const Node &now){ this->mu = now.mu ; this->zi = now.zi ; return *this ; } friend ostream & operator << (ostream &out , const Node &A){ out<<A.zi<<"/"<<A.mu ; return out ; } }; int main(){ int t ; LL x ; cin>>t ; while(t--){ cin>>x ; LL v = V(x) ; LL u = U(x) ; Node ans = Node(1 , 2) - Node(1 , v) ; Node sum = Node(x-v+1, u*v) + ans ; cout<<sum<<endl ; } return 0; }
相关文章推荐
- 题目1548:平面上的点 (map 里面放结构体)
- 载入背景图片
- ADO数据库操作
- 智能指针 ADO数据库连接
- 线段树专题
- 你不理财,财不理你大全集
- Oracle数据库精讲与疑难解析
- 字符串逆序输出
- Top 10 Computer Tricks Every Geek Should Know
- Zoj3762 等待解决
- 程序在内存中的分配
- 内部排序之四:归并排序和快速排序(含完整源码)
- 深入解析Oracle.DBA入门进阶与诊断案例
- Oracle.11g权威指南 第2版)].谷长勇
- oracle实例恢复 详解理论知识
- struts.xml配置json
- Ural 1091 Tmutarakan Exams 解题报告(容斥原理)
- printf格式控制符
- Oracle10g服务器管理恢复
- 0.ring0-PAE-(虚拟地址转换成物理地址详细示例)