题目链接: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. JProtector java应用加密工具

    JProtector    专业的java项目加密工具 JProtector简介: JProtector 专业的java项目加密工具.目前java开发的项目发布的时候需要将项目发布到用户手中,但由于一 ...

  2. Hibernate JPA 中配置Ehcache二级缓存

    在Hibernate3 JPA里配置了一下非分布式环境的二级缓存,效果不错.具体过程如下: 1, 需要引入的jar包 http://ehcache.org/downloads/catalog 下载的包 ...

  3. linux下ip命令用法

    配置数据转发,可以通过 1.路由转发即用用路由器实现: 2.使用NAT转发: 简单的说: 路由表内的信息只是指定数据包在路由器内的下一个去处.并不能改变数据包本身的地址信息.即它只是“换条路而已,目的 ...

  4. 关于怎么C#控制台窗口中怎么创建连接查询数据库操作

    首先需要新建一张表,为了测试随建了一张学生表 新建号一张表之后就可以对数据库进行操作了 列举了常用的增删改查 操作 static void Main(string[] args)        { s ...

  5. Qt学习之路:自定义Model三篇,自定义委托等等

    http://devbean.blog.51cto.com/448512/d-8/p-2

  6. QList 和std::list的比较

    QList QList<T> 是一个Qt通用容器类.它存储一序列的值,并且提供基于索引的数据访问方法和快速的插入和删除操作. QList<T>, QLinkedList< ...

  7. CairoSVG - Convert SVG to PNG or PDF - Contents

    CairoSVG - Convert SVG to PNG or PDF - Contents User Documentation Author Guillaume Ayoub Date 2011- ...

  8. xcode APP 打包以及提交apple审核详细流程(新版本更新提交审核)

    链接地址:http://blog.csdn.net/mad1989/article/details/8167529 打包发布APP流程真机测试和APP发布流程APP提交审核流程真机测试打包发布上传出错 ...

  9. Python 获取时间戳

    Python 获取时间通过 time 模块 如下代码,是通过获取当前的时间,按照格式输出 Python默认获取当前的时间返回的都是时间的元组,下面是元组的,字符串时间的一个转换输出 # -*- cod ...

  10. spring mvc 和 jstl

    spring ,jstl 在maven配置文件的配置:<dependency><groupId>org.springframework</groupId><a ...