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 大意: ...
随机推荐
- HDU 4857 逃生 (反向拓扑排序 & 容器实现)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- cache缓存帮助类
public class CacheHelper { /// <summary> /// 创建缓存项的文件 /// </summary> /// <param name= ...
- web安全之sql注入的防御
自动把引号转义 1.防御sql注入的基本原则 任何时候不应该改变用户的输入 比如用户输入单引号,那输出也要是单引号. 几种基本的防 ...
- 特别实用而且功能强大的attributedText属性
UILabel *sendNameLB = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; NSString * string = @" ...
- grep命令学习
grep(Globally search a Regular Expression and Print), 全面搜索正则表达式并把行打印出来,是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把 ...
- circular_buffer
编程有时需要使用定长的容器(fixed size container).实现旋转容器可以像下面这样: std::vector<T> vec(size); vec[i % size] = n ...
- [课程设计]Scrum 1.4 多鱼点餐系统开发进度
Scrum 1.4 多鱼点餐系统开发进度 (点餐页面框架布置) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐系 ...
- 数迹学——Asp.Net MVC4入门指南(5):从控制器访问数据模型
MovieController中的方法Index()代码,初认识,应该有很多理解错误的地方,暂时这么记忆吧,待随后修改 Index()代码: @model IEnumerable<MVCMovi ...
- C#子类调用基类构造备忘
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace First ...
- javascript标识符
标识符,就是指变量.函数.属性的名字,或者函数的参数. 规则 1.第一个字符必须是一个字母.下划线或是美元符号($) 2.其他字符可以是字母.下划线.美元符号或数字 3.不能是关键字和保留字 4.区分 ...