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 ...
随机推荐
- Entity Framework插入数据报错:Validation failed for one or more entities
www.111cn.net 编辑:lanve 来源:转载 今天在处理Entity Framework插入数据库时,报错: Validation failed for one or more entit ...
- (转)SpringMVC学习(三)——SpringMVC的配置文件
http://blog.csdn.net/yerenyuan_pku/article/details/72231527 读者阅读过SpringMVC学习(一)——SpringMVC介绍与入门这篇文章后 ...
- 油猴Tampermonkey 全局函数 它的注入函数都在 onload里面,直接写函数 都是内部函数,外部要是调用,就要挂靠到window上
油猴Tampermonkey 全局函数 它的注入函数都在 onload里面,直接写函数 都是内部函数,外部要是调用,就要挂靠到window上 window.like111 = function (){ ...
- Airbnb:别抵制我,宝宝要过 10 岁生日
今日导读 喜欢旅游的你,一定听说或使用过 airbnb(爱彼迎),在出发前打开它,总是能通过它开始一段奇妙的旅行.可是最近,就在这个打着“共享家”概念的服务型网站正迎来十周年之际,它却遭到了很多国家的 ...
- [NOI2010]海拔——最小割+对偶图
题目链接 SOLUTION 想一下最优情况下肯定让平路或下坡尽量多,于是不难想到这样构图:包括左上角的一部分全部为\(0\),包括右下角的一部分全部为\(1\),于是现在问题转化为求那个分界线是什么. ...
- RedHat7搭建KVM虚拟机
RedHat7搭建KVM虚拟机 1. 宿主机安装RedHat7.3系统 1.1选择语言 中文.简体中文(中国) 1.2安装位置 1.2.1自定义分区,选择LVM,将分区空间全部分配给根 1.2.2禁用 ...
- java在线聊天项目0.9版 实现把服务端接收到的信息返回给每一个客户端窗口中显示功能之客户端接收
客户端要不断接收服务端发来的信息 与服务端不断接收客户端发来信息相同,使用线程的方法,在线程中循环接收 客户端修改后代码如下: package com.swift; import java.awt.B ...
- 接口和类方法中的 SELF
接口和类方法中的 SELF 由 王巍 (@ONEVCAT) 发布于 2015/06/10 我们在看一些接口的定义时,可能会注意到出现了首字母大写的 Self 出现在类型的位置上: protocol I ...
- 目录扫描工具DirBuster
DirBuster是用来探测web服务器上的目录和隐藏文件的.因为DirBuster是采用java编写的,所以运行前要安装上java的环境. 来看一下基本的使用: ①:TargetURL下输入要探测网 ...
- Springboot(二)-application.yml默认的配置项以及读取自定义配置
写在前面 ===== spring-boot 版本:2.0.0.RELEASE ===== 读取自定义配置 1.配置文件:sys.properties supply.place=云南 supply.c ...