Codeforces Round #232 (Div. 2) 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
.
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).
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.
2
2
3
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;
}
Codeforces Round #232 (Div. 2) D. On Sum of Fractions的更多相关文章
- Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)
Problem Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...
- Codeforces Round #232 (Div. 1)
这次运气比较好,做出两题.本来是冲着第3题可以cdq分治做的,却没想出来,明天再想好了. A. On Number of Decompositions into Multipliers 题意:n个数a ...
- Codeforces Round #232 (Div. 2) 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 pri ...
- Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp
B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...
- Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳
E. Product Sum 题目连接: http://www.codeforces.com/contest/631/problem/E Description Blake is the boss o ...
- Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)
传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...
- Codeforces Round #238 (Div. 2) D. Toy Sum 暴搜
题目链接: 题目 D. Toy Sum time limit per test:1 second memory limit per test:256 megabytes 问题描述 Little Chr ...
- Codeforces Round #232 (Div. 2) B. On Corruption and Numbers
题目:http://codeforces.com/contest/397/problem/B 题意:给一个n ,求能不能在[l, r]的区间内的数字相加得到, 数字可多次重复.. 比赛的时候没有想出来 ...
- Codeforces Round #232 (Div. 1) A 解题报告
A. On Number of Decompositions into Multipliers 题目连接:http://codeforces.com/contest/396/problem/A 大意: ...
随机推荐
- php学习前的准备
1.用户文档: 官方中文文档:http://www.php.net/manual/zh/ 官方扩展库:http://pecl.php.net/packages.php
- Eclipse 中 Tomcat启动卡100%(preparing launch delegate...)
我自己遇到这个问题的时候去百度了好几天,没找到我的解决方案,因为我的错误和别人不一样,但提示却和别人一样,在tomcat启动到100%的时候,卡住了,最后显示45秒不够启动,建议我增加时间,所以结果可 ...
- DP(Dynamic programming)——尽力学习之中(2016 HUAS ACM 暑假集训-5)
这周不打算按照以往的方式更新博客,而是采用整体的方式.一是因为学的太少,没东西写:二是这篇博客会经常更新的.如题,DP——尽力学习之中. ------------------------------- ...
- 2016HUAS_ACM暑假集训4A - 递推
利用组合公式C(n,m)=C(n-1,m)+C(n-1,m-1).也就是从n个数里面选择m个数.按递增方式放在每一层循环. 杨辉三角+二项式定理,还真是挺有“意思”的一道题.说实话,非原创.见谅... ...
- python字符串基础知识
1.python字符串可以用"aaa",'aaa',"""aaa""这三种方式来表示 2.python中的转义字符串为" ...
- Qt and C++ Reflection,利用Qt简化C++的反射实现
如何在C++中实现反射机制,应该算是C++开发中经常遇到的问题之一.C++程序没有完整的元数据,也就无法实现原生的反射机制.从性能的角度讲,这样的设计不难理解,毕竟在运行时储存这些元数据需要额外的开销 ...
- MsXml创建和解析XML示例
一.MsXml创建XML文档示例 // XmlCreationDemo.cpp #include <stdlib.h> #include <stdio.h> // 引入MSXM ...
- Android studio 快捷键(Mac)
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- 白电迁移-Auto fdisk
============================== fdisk /dev/vdbnp 1 w fdisk -l cd / mkdir /data mkfs.ext4 /dev/vdb1 mo ...
- [转]VMware Workstation网络连接的三种模式
经常要使用VMWare Workstation来在本地测试不同的操作系统,以前也搞不清楚网络连接三种模式,最近看了几篇文章才算明白.现总结如下: 1. VMware Workstation的虚拟网络组 ...