题目链接: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)
时间复杂度。<sqrt(n)时 O(1),>sqrt(n)时,O(n)
2、假设乍一看没发现规律或者没思路。自己模拟几个数。连续着模拟。别局限于Sample input。自己找找规律
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 数论的更多相关文章

  1. 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 ...

  2. codeforces 963A Alternating Sum

    codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...

  3. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  4. codeforces 616E Sum of Remainders (数论,找规律)

    E. Sum of Remainders time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. Codeforces 622F The Sum of the k-th Powers(数论)

    题目链接 The Sum of the k-th Powers 其实我也不懂为什么这么做的……看了无数题解觉得好厉害哇…… #include <bits/stdc++.h> using n ...

  6. CodeForces 743C Vladik and fractions (数论)

    题意:给定n,求三个不同的数满足,2/n = 1/x + 1/y + 1/z. 析:首先1是没有解的,然后其他解都可以这样来表示 1/n, 1/(n+1), 1/(n*(n+1)),这三个解. 代码如 ...

  7. 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 ...

  8. Codeforces 963A Alternating Sum ( 思维 && 数论 )

    题意 : 题目链接 分析 : Tutorial 讲的很清楚 至于为什么这样去考虑 算是一个经验问题吧 如果一个问题要你给出模意义下的答案 就多考虑一下答案是要用逆元构造出来 也就说明有除法的存在 那么 ...

  9. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

随机推荐

  1. QTableWidget的用法总结

    在使用Qt不多的日子里,已经两次用到了QTableWidget这个控件,也慢慢的习惯和喜欢上了它.再使用QTableWidget的时候,已不像刚开始使用时的迷茫.嗯嗯.现在就来总结总结我与QTable ...

  2. docker学习笔记:修改无法启动的容器中的内容

    我们可能会碰到这样的一个问题,在容器执行过程中,修改了容器的内容(如配置文件信息),但因为修改出了问题.导致容器关闭后,无法启动. 这事需要重新修改配置文件. 正常情况下可以通过 docker exe ...

  3. python IDE ulipad配置使用

    一直认为认为python自带的编辑器idle实在是太简陋了,连显示行号的功能都没有,也非常不好编辑.找了下windows平台下的Ide,发现ulipad很多人推荐使用,就开始安装了. 首先去官网: h ...

  4. 基于visual Studio2013解决算法导论之017查找第n小元素

     题目 查找第n小元素 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> ...

  5. Pison geeker

    Pison on scriptogr.am Pison Abraham Lincoln: "Nearly all men can stand adversity, but if you wa ...

  6. 开源 免费 java CMS - FreeCMS1.9 职位管理

    项目地址:http://code.google.com/p/freecms/ 职位管理 管理职位,实现招聘功能. 1. 职位管理 从左側管理菜单点击职位管理进入. 2. 加入职位 在职位列表下方点击& ...

  7. Servlet 实现文件的上传与下载

    这段时间尝试写了一个小web项目,其中涉及到文件上传与下载,虽然网上有很多成熟的框架供使用,但为了学习我还是选择了自己编写相关的代码.当中遇到了很多问题,所以在此这分享完整的上传与下载代码供大家借鉴. ...

  8. SQL基础使用

    result = executeQuery(“ SELECT FName, FSalary FROM T_Employee ”); for(i=0;i<result.count;i++) { s ...

  9. SecureCRT使用Vim出现中文乱码问题的解决

    1. 首先保证securecrt本身显示中文是ok的.如果不是,就先解决这一个问题. 2. vi ~/.vimrc 添加set encoding=utf-8 fileencodings=ucs-bom ...

  10. Http方式获取网络数据

    通过以下代码可以根据网址获取网页的html数据,安卓中获取网络数据的时候会用到,而且会用Java中的sax方式解析获取到数据.(sax解析主要是解析xml)具体代码如下: package com.wy ...