Codeforces 396B On Sum of Fractions 规律题

题目链接:点击打开链接

我们把 1 / { u(i)*v(i) }拆开->  (1/(u(i)-v(i)) * ( 1/v(i) - 1/u(i) )

若n +1  是素数,则显然(1/(u(i)-v(i)) * ( 1/v(i) - 1/u(i) ) 这样完全相同的式子有 u(i)-v(i) 个

那么就可以把前面系数约掉,那么剩下的式子就是 1/2 - 1/(n+1)

若不是,则我们找到第一个<=n的素数,即v(n)

和第一个>n的素数,即 u(n)

然后前面的 2-v(n)求和,即 1/2 - 1/v(n)

后面共有 n - v(n)项,即 (n-v(n)) * (1/ (u(n)*(v(n) )

同分即可。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<queue>
#include<map>
#include<vector>
using namespace std;
#define N 10000
#define ll __int64
inline ll gcd(ll x, ll y){
	if(x>y)swap(x,y);
	while(x){
		y %= x;
		swap(x,y);
	}
	return y;
}
ll prime[N],primenum;//有primenum个素数 math.h
void PRIME(ll Max_Prime){
    primenum=0;
    prime[primenum++]=2;
    for(ll i=3;i<=Max_Prime;i+=2)
    for(ll j=0;j<primenum;j++)
        if(i%prime[j]==0)break;
        else if(prime[j]>sqrt((double)i) || j==primenum-1)
        {
            prime[primenum++]=i;
            break;
        }
}
bool Isprime(ll x){
	ll maxx = sqrt(x)+10;
	maxx = min(maxx, x);
	for(ll i = 1; i < primenum && prime[i]<maxx; i++)
		if(x%prime[i] == 0)return false;
	return true;
}
ll n;
ll Havprime(ll x, ll add){
	if(x==2)return x;
	if(!(x&1))x+=add;
	add <<= 1;
	while(1){
		if(Isprime(x))return x;
		x += add;
	}
}
ll s,x;
void go(ll u, ll v){
	if(v==u-1){
		s = u-2;	x = 2*u;
		return;
	}
	x = 2*u*v;
	s = u*v-2*u-2*v+2*n+2;
}
int main(){
	PRIME(100000);
	ll i, u, j, T;cin>>T;
	while(T--) {
		cin>>n;
		ll v = Havprime(n,-1), u = Havprime(n+1,1);
		go(u,v);
		ll _gcd = gcd(s,x);
		cout<<s/_gcd<<"/"<<x/_gcd<<endl;
	}
	return 0;
}

Codeforces 396B On Sum of Fractions 规律题

时间: 2024-10-12 13:34:55

Codeforces 396B On Sum of Fractions 规律题的相关文章

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,暴力是不可能了,没什么思路,后来在纸上列了几项,突然想到高中时候求等差数列时候用到

Codeforces 10C Digital Root 规律题

题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<string> #include<stdlib.h> #include<algorithm> using nam

ZOJ 3798 Abs Problem(规律题)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3798 Abs Problem Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Alice and Bob is playing a game, and this time the game is all about the absolute value! Alice has

HDU 4279 Number 规律题

题意: 定义函数F(x) : 区间[1,x]上的y是满足:GCD(x,y)>1 && x%y>0的 y的个数. 问:对于任意区间[l,r] 上的F(l···r) 有几个函数值是奇数的. 打表找规律. 打的是[1,x]区间的结果 把所有结果不相同的值打出来(因为结果是递增的,所以只观察不相同的结果) 发现:ans = x/2-2 || x/2-1 再把所有结果不同 && x/2-1的值打出来 发现 sqrt(x) &1 == 1 得到:ans = x/2-

LightOJ1010---Knights in Chessboard (规律题)

Given an m x n chessboard where you want to place chess knights. You have to find the number of maximum knights that can be placed in the chessboard such that no two knights attack each other. Those who are not familiar with chess knights, note that

hdu5351 MZL&#39;s Border(规律题,java)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud MZL's Border Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 905    Accepted Submission(s): 295 Problem Description As is known to all,

HDU_1098 Ignatius&#39;s puzzle[规律题]

Ignatius's puzzle Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice but to appeal to Eddy. this problem describes that:f(x)=5*x^13+13*x^5+k*a*x,input a nonegative integer k(k<10000),to find the minimal

codeforces 459C - Pashmak and Buses 【构造题】

题目:codeforces 459C - Pashmak and Buses 题意:给出n个人,然后k辆车,d天时间,然后每天让n个人选择坐一辆车去上学,要去d天不能有任意两个人乘同一辆车,不能的话输出 -1 分类:数学,构造 分析:这个题目首先得分析,我开始想到的是首先用相同的放在一起,比如 7 2 3 这样构造 1 1 1 1 2 2 2 1 1 1 2 2 2 1 1 1 2 2 2 1 1 1 2 2 2 1 1 1 就是需要的天数跟每一行出现次数最多的数的出现次数相等,但是发现还有更优

【Codeforces 368A】Brain&#39;s Photos 水题

黑白灰都是#Black&White #include <cstdio> int n,m; int main() { scanf("%d%d",&n,&m); int ok=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { char s[5]; scanf("%s",s); if(s[0]!='W'&&s[0]!='B'&&s[0]!='G')