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 大意: ...
随机推荐
- 接触到得到新语言里面涉及到很多关于ECMscript相关知识,所以还是来总结一下吧
ES6新增加了一些特性下面我就一边回忆一边写博文吧. 1.es6里面给我印象最深的应该就是箭头操作符(=>),它简化了函数的书写,第一次看见的时候完全懵逼,然后百度后才知道.操作符左边为输入的参 ...
- Flask 的扩展
1. Flask-Script,为Flask程序提供了一个命令行解析器: (venv) $ pip install flask-script 2. Bootstrap(http://getbootst ...
- redis学习
wget http://labfile.oss.aliyuncs.com/files0422/redis-2.8.9.tar.gz .tar.gz cd redis- make make instal ...
- PSP第九周
一.表格 C(分类) C(内容) S(开始时间) ST(结束时间) I(打断时间) △(净工作时间) 学习 UML 12:30 13:20 0 50 编码 编码 20:00 22:10 0 130 学 ...
- Web前端开发学习心得(一)
说快也快,说慢也慢.自学前端一个月了,进步不算大,不过仔细想来,倒也收获不少.码几行字,一是总结经验,二是继往开来. 刚开始是跟慕课网学的HTML+CSS,慕课网的课程虽然好,但好多知识都是特别基础的 ...
- windows下安装Composer
1.下载https://getcomposer.org/composer.phar 2.复制到php.exe所在目录 3.在php.exe所在目录新建composer.bat文件 4.打开cmd,跳转 ...
- linux云服务器mysql ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’
一早上过来发现网站打开报错,数据库连接不上.. 有人改密码? putty进去,mysql -uroot -p 输入密码后,报错 ERROR 2002 (HY000): Can't connect to ...
- sokite
<?php interface Proto { //连接 function conn($url); //发送get请求 function get(); //发送post请求 function p ...
- List的FindIndex和ForEach
FindIndex和ForEach能接受一个delegate,用来定义查找规则太好用. 匿名函数,萌萌哒:Conditional("DEBUG")也是极好的. 读<C#本质论 ...
- ILGenerator.Emit动态 MSIL编程(一)之基础
首先在Framework中,Emit相关的类基本都存在于System.Reflection.Emit命名空间下.可见Emit是作为反射的一个元素存在的. Emit能够实现什么?为什么要学习Emit?首 ...