cf396B 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
写写1/v(i)u(i)的前几项就能发现规律
i 2 3 4 5
v 2 3 3 5
u 3 5 5 7
如果用个f(i)表示1/v(i)u(i),那么对于一个质数x,有f(2)+f(3)+...+f(x-1)=1/2-1/x
然后在对于一个夹在两质数a,b之间的x,显然从a到x的f值都是a/b,所以就是找到n前后最近的质数,把两分式通分一下就好了
这里我是直接n往前往后Miller-Robin找第一个质数,不过sqrt(n)的暴力应该也能卡过去?
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL mul(LL x,LL n,LL MOD)
{
LL res=x*n-(LL)((long double) x*n/MOD+0.5)*MOD;
while (res<)res+=MOD;
while (res>=MOD)res-=MOD;
return res;
}
LL qpow(LL x,LL n,LL MOD)
{
x=(x%MOD+MOD)%MOD;
LL p=x,con=;
while (n)
{
if (n&)con=mul(con,p,MOD);
p=mul(p,p,MOD);
n>>=;
}
return con;
}
bool witness(LL a,LL b)
{
if (a==b)return true;
LL s=b-;
int t=;
while (!(s&))s>>=,t++;
LL x=qpow(a,s,b);
if (x==)return ;
while (t--)
{
if (x==b-)return true;
x=mul(x,x,b);
if (x==)return false;
}
return false;
}
bool isprime(LL x)
{
if (x==||x==)return false;
static int p[]={,,,,,,,,,,};
for (int i=;i<=;i++)
if (!witness(p[i],x))return false;
return true;
}
inline LL gcd(LL a,LL b)
{
if (a<b)swap(a,b);
return b==?a:gcd(b,a%b);
}
int main()
{
int T=read();
while (T--)
{
LL x=read(),y,z,t,ans1,ans2;
for (y=x;y>=;y--)if (isprime(y))break;
for (z=x+;z<=1e9+;z++)if (isprime(z))break;
//ans=1/2-1/y+(x-y+1)*y/z
ans1=y*z-*z+*x-*y+;ans2=*y*z;
t=gcd(ans1,ans2);ans1/=t;ans2/=t;
printf("%lld/%lld\n",ans1,ans2);
}
}
cf396B
cf396B On Sum of Fractions的更多相关文章
- Codeforces Round #232 (Div. 2) D. On Sum of Fractions
D. On Sum of Fractions Let's assume that v(n) is the largest prime number, that does not exceed n; u ...
- Codeforces 396B On Sum of Fractions 数论
题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...
- cf D. On Sum of Fractions
http://codeforces.com/problemset/problem/397/D 题意:v(n) 表示小于等于n的最大素数,u(n)表示比n的大的第一个素数,然后求出: 思路:把分数拆分成 ...
- 数学题--On Sum of Fractions
题目链接 题目意思: 定义v(n)是不超过n的最大素数, u(n)是大于n的最小素数. 以分数形式"p/q"输出 sigma(i = 2 to n) (1 / (v(i)*u(i) ...
- 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 Round #232 (Div. 1)
这次运气比较好,做出两题.本来是冲着第3题可以cdq分治做的,却没想出来,明天再想好了. A. On Number of Decompositions into Multipliers 题意:n个数a ...
- WannaflyUnion每日一题
---恢复内容开始--- 1. http://www.spoj.com/problems/KAOS/ 题意:给定n个字符串,统计字符串(s1, s2)的对数,使得s1的字典序比s2的字典序要大,s1反 ...
- Codeforces Round #384 (Div. 2) C. Vladik and fractions(构造题)
传送门 Description Vladik and Chloe decided to determine who of them is better at math. Vladik claimed ...
- 模拟 --- hdu 12878 : Fun With Fractions
Fun With Fractions Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit ...
随机推荐
- Grace Huang 2017/1/11
原文 This actress becomes each character she plays Grace Huang has no interested in doing same thing y ...
- perl在linux下通过date获取当前时间
perl处理文件的时候最好添加上 处理的时间戳,获取系统的时间又多种方法,但是反引号是最原始的,不需要其他外界条件和lib的支持. my $now = `date "+%F %T" ...
- 洛谷 P2935 [USACO09JAN]最好的地方Best Spot
题目描述 Bessie, always wishing to optimize her life, has realized that she really enjoys visiting F (1 ...
- Xcode - 'openssl/opensslconf.h' file not found解决
点击Build Settings搜索Header Search Paths,添加$(SRCROOT)/目录/Alipay
- Navicat连接Oracle详细教程
Navicat Premium算是比较好的一个可视化数据库管理工具了,短小精悍,一个工具解决三种数据库的连接问题,真正做到了集成管理,对MySQL,SQLServer而言,连接比较简单,就不赘述了,现 ...
- JavaScript判断数组是否包含指定元素的方法
本文实例讲述了JavaScript判断数组是否包含指定元素的方法.分享给大家供大家参考.具体如下: 这段代码通过prototype定义了数组方法,这样就可以在任意数组调用contains方法 /** ...
- C++基础:虚函数、重载、覆盖、隐藏<转>
转自:http://www.2cto.com/kf/201404/291772.html 虚函数总是跟多态联系在一起,引入虚函数可以使用基类指针对继承类对象进行操作! 虚函数:继承接口(函数名,参数, ...
- 三、绘图和可视化之matplotlib
#matplotlib简单绘图之plot import matplotlib.pyplot as plt a=[1,2,3] b=[10,2,30] plt.plot(a)#纵坐标为a的值,横坐标为a ...
- CPP-基础:字节对齐
一. 什么是字节对齐,为什么要对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特定的 ...
- QT +样式表
学习样式表的目的:可以设计出好看的控件.(比如可以给一些按钮设计成好看的图片) QT 样式表的思想很大程度上是来自于HTML的层叠式样式表(CSS),通过调用QWidget->setStyleS ...