Codeforces 396B on Sum of fractions number theory

Source: Internet
Author: User

Title Link: codeforces 396B on Sum of fractions

From: http://blog.csdn.net/keshuai19940722/article/details/20076297

Title: Give a N,ans =∑ (2≤i≤n) 1/(V (i) *u (i)), V (i) is not greater than the maximum number of primes, u (i) is greater than the smallest prime, for ans, the output in fractional form.


Problem-solving ideas: A beginning to see this problem 1e9, violence is impossible, no idea, and later on the paper on a few items, suddenly thought of high school when asked arithmetic progression time to use the method. I don't remember the details.

1/(2*3) = (1/2-1/3) * 1/(3-2);

1/(3*5) = (1/3-1/5) * 1/(5-3);

Then the number of values is 1/(2*3) has (3-2), 1/(3*5) has (5-3).

So if there is n,v = V (n), u = u (n);

1/(2*3) + 1/(3*5) * (5-3) + ... + 1/(v*u) * (n-v+1) (note that the last one is not u-v)

= 1/3 + 1/3-1/5 + ... -1/v + 1/(v*u) * (n-v+1)

= 1/v + 1/(v*u) * (n-v+1)

p = u*v + (n-v-u+1); Q = 2*u*v;

Remember numerator, and then you and V are enumerated in the way.

Learn to:
1, to find a very large number of n nearest to his prime, be able to sift out the prime in sqrt (n), and then, the inferred prime is not an approximate number of n---in fact, with the help of a pair of approximate, small always <=sqrt (n)complexity of time. <SQRT (N) O (1), >SQRT (n), O (n )2, suppose at first glance did not find the law or no train of thought. Simulate several numbers yourself. A continuous simulation. not limited to sample input. Find the rules for yourself .

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 Code

#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#de Fine 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;}




Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Codeforces 396B on Sum of fractions number theory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.