题目链接:Codeforces 396B On Sum of Fractions
题目大意:给出一个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就用枚举的方式。
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int N = 1e5+10;
typedef long long ll;
int cnt, prime[N], flag[N];
void init () {
cnt = 0;
memset(flag, 0, sizeof(flag));
for (int i = 2; i < N; i++) if (!flag[i]) {
prime[cnt++] = i;
for (int j = i*2; j < N; j += i) flag[j] = 1;
}
}
bool isPrime (int k) {
if (k < N) return !flag[k];
for (int i = 0; i < cnt && prime[i] <= k; i++)
if (k%prime[i] == 0) return false;
return true;
}
ll under (int n) {
for (int i = n; i >= 2; i--)
if (isPrime(i)) return i;
return 0;
}
ll up (int n) {
for (int i = n + 1; i; i++)
if (isPrime(i)) return i;
return 0;
}
int gcd(ll p, ll q) {
if (!q) return p;
else return gcd(q, p%q);
}
int main () {
init();
int cas, n;
scanf("%d", &cas);
while (cas--) {
scanf("%d", &n);
ll v = under(n);
ll u = up(n);
ll p = u*v + 2*(n-v-u+1);
ll q = 2*u*v;
ll d = gcd(p, q);
cout << p/d << "/" << q/d << endl;
}
return 0;
}
分享到:
相关推荐
Codeforces - 1107B. Digital root & 1107C. Brutality(规律 & 贪心)Codeforces - 1107B.
Codeforces 题库 101-200 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
Codeforces 1105B - Zuhair and Strings 测试点37个(全)
Codeforces 题库 001-100 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
codeforces编程网站预测分数插件
使用于Google Chrome的Codeforces Enhancer 1.1.2插件安装包。 版本:codeforces enhancer 1.1.2 使用浏览器:Google Chrome
Some of the Codeforces problems codes
Codeforces 185A - Plant 全测试点49个
codeforces 19 E Fairy 一道比较难的题目的解题报告 推荐阅读
接受串子-接受字符串相等-接受Codeforces回合#684(Div.2) 2/6 1440A-购买琴弦-接受1440B -中位数的总和-已接受1440C1-二进制表(简易版)-已接受1440C2-二进制表(硬版)-已接受 Codeforces回合#683(分区2) 1/...
Codeforces global round 10 codes
Codeforces round 678 division 2 codes
lucifer1004大佬的博客cf上分攻略故里大佬的githubcf思维题刷题数:44- (1421)codeforces 676 div2 A,B done
Codeforces round 678 D2_Codeforces_源码
打codeforces的神器
一个Codeforces、牛客竞赛、AtCoder平台的编程竞赛查询插件,ACMer必备.zip
codeforces-js Codeforces JS
codeforces每日一题。 题意: 给出一个数组,让你挑选出能组成任意pair差值为2的幂的序列,并且使这个序列长度尽可能大。 思路: 针对于挑选出来的序列,任意pair对的差值为2的幂数。 假设有多个pair对,设dis(a,b)=2...
使用 C# + WPF 开发 --- 还在发愁打了那么多场比赛都没有进入首页么? 还在为了前 5 的 hacker 名额阅读千份代码么? 是的,你没有看错! 这是一个 Edu & Div.3 轮 Open hacking 错误代码自动查找器!...
Codeforces Round #723 (Div. 2).md