codeforce Round On Sum of Fractions
来源:互联网 发布:文明mac版 编辑:程序博客网 时间:2024/05/17 20:32
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
2
2
3
output:1/6
7/30
1:欧几里得算法 计算GCD递归
gcd(a,b) == gcd(b,a%b); a,b>0
2:判断素数:
3:符号重载
1 #include<iostream> 2 #include<stdio.h> 3 #include<string> 4 #include<string.h> 5 #include<map> 6 #include<math.h> 7 8 using namespace std; 9 typedef long long LL;10 bool Is_Prime(LL x)11 {12 for(LL i=2;i*i<=x;i++)13 if(x%i == 0)14 return 0;15 return 1;16 }17 LL V_Down(LL x)18 {19 while(!Is_Prime(x))20 x--;21 return x;22 }23 LL U_Up(LL x)24 {25 x++;26 while(!Is_Prime(x))27 x++;28 return x;29 }30 LL gcd(LL x, LL y)31 {32 return y==0?x:gcd(y, x%y);33 }34 class Node35 {36 public:37 LL zi;38 LL mu;39 public:40 Node(){};41 Node(LL z ,LL m)42 {43 LL g=gcd(z,m);44 zi=z/g;45 mu=m/g;46 };47 Node operator +(const Node &A)const48 {49 LL m,z,g;50 g=gcd(mu,A.mu);51 m=mu/g*A.mu;52 z=A.mu/g*zi+mu/g*A.zi;53 g=gcd(z,m);54 return Node(z/g,m/g);55 }56 Node operator - (const Node & A)const57 {58 LL m,z,g;59 g=gcd(mu,A.mu);60 m=mu/g*A.mu;61 z=A.mu/g*zi-mu/g*A.zi;62 g=gcd(z,m);63 return Node(z/g,m/g);64 }65 Node &operator = (const Node &now)66 {67 this->mu=now.mu;68 this->zi=now.zi;69 return *this;70 }71 72 friend ostream & operator <<(ostream &os,const Node &A)73 {74 os<<A.zi<<"/"<<A.mu;75 return os;76 }77 78 79 };80 int main()81 {82 int t;83 LL x;84 cin>>t;85 while(t--)86 {87 cin>>x;88 LL v=V_Down(x);89 LL u=U_Up(x);90 Node ans=Node(1,2)-Node(1,v);91 Node sum=Node(x-v+1,u*v)+ans;92 cout<<sum<<endl;93 }94 return 0 ;95 }
0 0
- codeforce Round On Sum of Fractions
- 数学题--On Sum of Fractions
- CF232 D2D:On Sum of Fractions,math
- codeforces#232_div2_D On Sum of Fractions
- B. On Sum of Fractions(数论)
- Codeforces 396B On Sum of Fractions(数论)
- Codeforces 396B On Sum of Fractions 规律题
- Codeforces 396B On Sum of Fractions 数论
- CodeForces 396 B.On Sum of Fractions(数论)
- Codeforce Round #336--B. Hamming Distance Sum
- Codeforce Round #115 Plane of Tanks: Pro
- Sereja and the Arrangement of Numbers -------Codeforce Round # 215 C
- Codeforce Round#350(Div. 2) 670B Game of Robots
- CodeForce 396A On Number of Decompositions into Multipliers
- CodeForce Round Table Knights
- codeforce round 175 div2
- codeforce round #217
- codeforce round#226 div2
- zoj 2412 dfs 求连通分量的个数
- ACE_Message_Block(V4.0)
- zoj 2110 dfs+步长
- 九度OJ 1548 map的用法
- codeforce Round #232 Div2 map + 质因子 +组合
- codeforce Round On Sum of Fractions
- 关于C#中集合和数组(三)
- hdu 数论专题
- hdu 4503 找全是朋友或全不是朋友
- hdu 4501 动态规划 + 费用背包问题
- hdu 1232 / 1213 并查集(求子集个数)
- hdu 1856 并查集 求最大的子树含有元素的个数
- poj 1182 并查集
- hdu 3635 并查集