题目链接: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. Linux内核启动及根文件系统载入过程

    上接博文<u-boot之u-boot-2009.11启动过程分析> Linux内核启动及文件系统载入过程 当u-boot開始运行bootcmd命令,就进入Linux内核启动阶段.与u-bo ...

  2. 关于apche无缘无故个启动不了,解决方法

    1. 对于用户不小心把apache下的conf文件不小心给修改了,可那会导致,启动不了apache, 解决办法可以重新下载一个, 64为  32位  下载地址 http://www.veryhuo.c ...

  3. WinForm - 格式化DataGridView单元格数据

    效果: 代码: /// <summary> /// 格式化数据 /// </summary> private void dataGridView1_CellFormatting ...

  4. InheritableThreadLocal

    InheritableThreadLocal继承自ThreadLocal,但比ThreadLocal多一个特性: 子线程可以继承父亲线程上下文中的信息 但是,有两个点需要注意的: 只有子线程创建之前的 ...

  5. BZOJ 1009: [HNOI2008]GT考试( dp + 矩阵快速幂 + kmp )

    写了一个早上...就因为把长度为m的也算进去了... dp(i, j)表示准考证号前i个字符匹配了不吉利数字前j个的方案数. kmp预处理, 然后对于j进行枚举, 对数字0~9也枚举算出f(i, j) ...

  6. 动态链接库dll的 静态加载 与 动态加载

    dll 两种链接方式  : 动态链接和静态链接(链接亦称加载) 动态链接是指在生成可执行文件时不将所有程序用到的函数链接到一个文件,因为有许多函数在操作系统带的dll文件中,当程序运行时直接从操作系统 ...

  7. 嵌入jetty到Java代码

    在做Demo实例时,使用的jetty版本号为8.x. 为了避免麻烦,将全部的包都导入到MyEclipse的lib文件夹下. 实例1:自己定义handler的服务器 package com.jetty. ...

  8. USB中CDC-ECM的了解和配置

    USB中典型类及子类: 类别 解释 子类 典型应用 IC芯片 备注 UVC 视频类 免驱USB摄像头 CDC 通讯类 RNDIS ECM(p24) 免驱USB网卡 RTL8152B EEM ..... ...

  9. C#调用存储过程实现分页(个人代码笔记)

    分页的存储过程: drop proc LoadPageMain create Proc LoadPageMain @pageIndex )) Fid     ) ].Rows )            ...

  10. flex调用webservice中的datatable结果写入datagrid

    webservice配置文件 <appSettings> <add key="sqlConDuke" value="server=10.9.34.88; ...