數學題--On Sum of Fractions

題目鏈接


題目意思:

定義v(n)是不超過n的最大素數, u(n)是大於n的最小素數。

以分數形式"p/q"輸出 sigma(i = 2 to n) (1 / (v(i)*u(i))), pq爲互質整數且q > 0。

通常會想到這個裂項。公式一寫發現約掉了許多。


最終是:

所以:AC 注意乘的過程會爆int。

#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>

using namespace std;
typedef long long LL;
const int INF=2e9+1e8;
const int MOD=1e9+7;
const int MAXSIZE=1e6+5;
const double eps=0.0000000001;
void fre()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
}
#define memst(a,b) memset(a,b,sizeof(a))
#define fr(i,a,n) for(int i=a;i<n;i++)

bool isprime(int n)
{
    for(int i=2;i*i<=n;i++) if(n%i==0) return false;
    return true;
}
LL gcd(LL a,LL b)
{
    return __gcd(a,b);
}
int main(int argc,char *argv[])
{
    int ncase;
    scanf("%d",&ncase);
    while(ncase--)
    {
        int n;
        scanf("%d",&n);
        int z,m;
        z=m=n;
        m++;
        while(!isprime(z)) z--;
        while(!isprime(m)) m++;
        LL fz,fm;
        fz=1ll*z*m-2ll*z-2ll*(m-n-1ll);
        fm=2ll*z*m;
        LL temp=gcd(fz,fm);
        cout<<fz/temp<<"/"<<fm/temp<<endl;
    }
    return 0;
}

/**************************************************/
/**             Copyright Notice                 **/
/**  writer: wurong                              **/
/**  school: nyist                               **/
/**  blog  : http://blog.csdn.net/wr_technology  **/
/**************************************************/



水一下

發佈了100 篇原創文章 · 獲贊 46 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章