Codeforces 396B On Sum of Fractions 数论
题目链接:Codeforces
396B On Sum of Fractions
题解来自:http://blog.csdn.net/keshuai19940722/article/details/20076297
题目大意:给出一个n,ans = ∑(2≤i≤n)1/(v(i)*u(i)), v(i)为不大于i的最大素数,u(i)为大于i的最小素数, 求ans,输出以分式形式。
解题思路:一開始看到这道题1e9,暴力是不可能了,没什么思路,后来在纸上列了几项,突然想到高中时候求等差数列时候用到的方法。详细叫什么不记得了。
1/(2*3) = (1/2 - 1/3) * 1/(3-2);
1/(3*5) = (1/3 - 1/5) * 1/(5-3);
然后值为1/(2*3)的个数有(3-2)个,为1/(3*5)的有(5-3)个。
这样如果有n,v = v(n), u = u(n);
1/(2*3) + 1/(3*5) * (5-3) + ...... + 1/(v*u) * (n-v+1) (注意最后不是u-v个)
= 1/2 - 1/3 + 1/3 - 1/5 + ........ -1/v + 1/(v*u) *(n-v+1)
= 1/2 - 1/v + 1/(v*u)*(n-v+1)
p = u*v + 2*(n-v-u+1); q = 2*u*v;
记得约分,然后u和v就用枚举的方式。
1、求一个非常大的数n的最接近他的素数,能够筛出sqrt(n)内的素数,然后,推断素数是不是n的约数---事实上是借助了一对约数里,小的总是<=sqrt(n)
int prmcnt;
bool is[N];int prm[M];
int getprm(int n)
{
int i,j,k=0;
int s,e=(int)(sqrt(0.0+n)+1);
CL(is,1);
prm[k++]=2;is[0]=is[1]=0;
for(i=4;i<n;i+=2)is[i]=0;
for(i=3;i<e;i+=2)
if(is[i])
{
prm[k++]=i;
for(s=i*2,j=i*i; j<n; j+=s)
is[j]=0;
}
for(;i<n;i+=2)if(is[i])prm[k++]=i;
return k;
} bool judge(int x)
{
if(x<MAXN-1)return is[x];
for(int i=0;i<prmcnt;i++)
if(x% prm[i] == 0)return 0;
return 1;
}
AC代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std; #define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout) const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const int INF = 100000000;
const int MAXN = 1e5+5;
const int N = MAXN;
const int M=N;
int prmcnt;
bool is[N];int prm[M];
int getprm(int n)
{
int i,j,k=0;
int s,e=(int)(sqrt(0.0+n)+1);
CL(is,1);
prm[k++]=2;is[0]=is[1]=0;
for(i=4;i<n;i+=2)is[i]=0;
for(i=3;i<e;i+=2)
if(is[i])
{
prm[k++]=i;
for(s=i*2,j=i*i; j<n; j+=s)
is[j]=0;
}
for(;i<n;i+=2)if(is[i])prm[k++]=i;
return k;
} bool judge(int x)
{
if(x<MAXN-1)return is[x];
for(int i=0;i<prmcnt;i++)
if(x% prm[i] == 0)return 0;
return 1;
} int calv(int x)
{
for(int i=x;i>1;i--)
if(judge(i))return i;
//
}
int calu(int x)
{
for(int i=x+1;;i++)
if(judge(i))return i;
} ll gcd(ll x, ll y)
{
return y == 0? x:gcd(y,x%y);
} int main()
{
prmcnt=getprm(MAXN-1);
int ncase,n;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d",&n);
ll v= calv(n);
ll u= calu(n);
ll up =v*u-2*u-2*v+2*n+2;
ll down=2*v*u;
ll tmp=gcd(up,down);
up/=tmp;
down/=tmp;
cout << up << '/' << down << endl;
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Codeforces 396B 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 963A Alternating Sum
codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- codeforces 616E Sum of Remainders (数论,找规律)
E. Sum of Remainders time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 622F The Sum of the k-th Powers(数论)
题目链接 The Sum of the k-th Powers 其实我也不懂为什么这么做的……看了无数题解觉得好厉害哇…… #include <bits/stdc++.h> using n ...
- CodeForces 743C Vladik and fractions (数论)
题意:给定n,求三个不同的数满足,2/n = 1/x + 1/y + 1/z. 析:首先1是没有解的,然后其他解都可以这样来表示 1/n, 1/(n+1), 1/(n*(n+1)),这三个解. 代码如 ...
- 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 963A Alternating Sum ( 思维 && 数论 )
题意 : 题目链接 分析 : Tutorial 讲的很清楚 至于为什么这样去考虑 算是一个经验问题吧 如果一个问题要你给出模意义下的答案 就多考虑一下答案是要用逆元构造出来 也就说明有除法的存在 那么 ...
- Codeforces Round #382 Div. 2【数论】
C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...
随机推荐
- JSON数组分配输出每个li
有这么一个JSON数组,需求是只需要输出每个数组里面的某个值,不需要全部输出来. var data = [ { ", "Cost":"13,642.41&quo ...
- 全球在一个 level 上思考的价值观和想法是一样的(转)
近日,福布斯中文版总编辑周建工对话马云,谈到腾讯频繁的大笔收购,马云点评称腾讯收购的所有的案子,老百性都看得懂,这就错了.战略就像买股票一样,如果老太太都开始买股票了,一定有问题. 以下是对话内容,转 ...
- ethtool命令
用途 显示或修改以太网卡的配置信息. 语法 ethtool [ -a | -c | -g | -i | -d | -k | -r | -S |] ethX ethtool [-A] ethX [aut ...
- Regionals 2012, Asia - Jakarta 解题报告
啥都不会做了.. 做题慢死 A.Grandpa's Walk 签到题. 直接DFS就行. 注意先判断这个点可以作为一个路径的起点不. 然后再DFS. 否则处理起来略麻烦 #include <io ...
- 手势滑动结束 Activity(一)基本功能的实现
喜欢听音乐的朋友可能都看过天天动听这款 app, 这款 app 有一个亮点就是在切换页面(Fragment)的时候能够通过手势滑动来结束当前页面.这里先说一下,我为什么会这么关心这个功能呢,由于前两天 ...
- UE-9260使用说明1
UE-9260使用说明 序号 版本号 日期 备注 1 V0.1 2015-03-21 原始版本号.作者:xiaobin 2 3 4 5 主机环境 1.烧写操作 仿真器和FTP烧写 OS: Win XP ...
- Unity3d 网络编程(二)(Unity3d内建网络各项參数介绍)
这里是全部Unity3d在网络中能用到相关的类及方法.纵观參数功能, Unity3d来写一个手游是不二的选择: RPC 能够传递的參数 int float string NetworkPlayer N ...
- ACM第三次比赛UVA11877 The Coco-Cola Store
Once upon a time, there is a special coco-cola store. If you return three empty bottles to the sho ...
- BZOJ AC 200题留念
话说本来想200AC就把题目总结一下...但是我现在挺懒的..不想弄...以后再来吧.
- SignalR系列教程:SignalR快速入门
---恢复内容开始--- 本篇是SignalR系列教程的第一篇,本篇内容介绍了如何创建SignalR应用,如何利用SignalR搭建简易的聊天室等,本篇内容参考自:http://www.asp.net ...