Math Count -2013 acm/icpc Asia Regional Changsha online-g question __ Math

Source: Internet
Author: User
Tags mul

Topic Link:

Http://acm.zju.edu.cn/changsha/showProblem.do?problemCode=G

The main effect of the topic:

To give a number x (1<x<=80000), you can use two kinds of operation addition and multiplication, ask up to three prime numbers, together to x how many methods together.

Ideas for solving problems:

Math count.

First, the number of prime screening method, the total number of 8,000, so use O (n^2) is OK.

And then discuss it in a separate situation.

1, only multiplication, a prime number multiplication, two prime numbers multiplied by three prime numbers multiplied. Note that the prime number is multiplied.

2, there is multiplication and addition. Now subtract a prime number and then determine whether it can be multiplied by two prime numbers.

3, addition of three prime numbers. Divided into three kinds of situation, do not want to wait, there are two the same, three are the same.

4, addition of two prime numbers. In two cases, not equal, equal.

First, the number of two different prime numbers consisting of and I, the same number of species.

The code explains in great detail:

Code:

#include <iostream> #include <cmath> #include <cstdio> #include <cstdlib> #include <string > #include <cstring> #include <algorithm> #include <vector> #include <map> #include <set > #include <stack> #include <list> #include <queue> #include <ctime> #define EPS 1e-6 #define INF 0x3f3f3f3f #define PI acos ( -1.0) #define LL long #define Lson l,m, (rt<<1) #define Rson m+1,r, (rt<<1) |

1 #pragma COMMENT (linker, "/stack:1024000000,1024000000") using namespace std;
#define MAXN 81000 bool PRIME[MAXN]; int pp[8000],cnt; After trying to get a total of 7,378 int num1[200000],num2[200000];
    Num1[i] represents and is a number of//num2[i with different prime numbers for I and a species void init () {memset (prime,true,sizeof (Prime)) consisting of the same prime number.
    prime[0]=0;
    prime[1]=0;
    int n=80000;
                for (int i=2;i<=sqrt (n*1.0), i++) {if (Prime[i]) {for (int j=i*2;j<=n;j+=i)
        prime[j]=0;
    }} cnt=0; for (int i=2;i<=n;i++) if (prime[i]) pp[++cnt]=i;
    for (int i=1;i<=cnt;i++)//o (8000^2) 8s almost for (int j=i+1;j<=cnt;j++) num1[pp[i]+pp[j]]++;
    for (int i=1;i<=cnt;i++) num2[pp[i]+pp[i]]++;
printf ("%d\n", CNT);
        the int mul (int n,int num)//is multiplied by the NUM prime number to get n {if (prime[n))//{if (num==1) return 1;
    else return 0;
    int res=0;
            for (int i=1;pp[i]*pp[i]<=n&&i<=cnt;i++) {while (n%pp[i]==0) {n/=pp[i];
            res++;
        if (Res>num) return 0;
    } if (n!=1) res++;
    if (res==num) return 1;

return 0;
   int main () {init ();
   int x;

   ll ans;
       while (~SCANF ("%d", &x)) {ans=0; Ans+=mul (x,1); A prime number multiplies Ans+=mul (x,2); Two prime numbers multiplied Ans+=mul (x,3);
          Three prime numbers multiplied//an addition and a multiplication for (int i=1;i<=cnt;i++) {int t=x-pp[i]; if (t<4) break;
       Ans+=mul (t,2);
       ///Three number of additions//three numbers are not the same ll temp=0;
               for (int i=1;i<=cnt;i++) {int t=x-pp[i];//Subtract the first if (t>=2) {
                        if (T-pp[i]>=2&&prime[t-pp[i]]) {if (x==pp[i]*3)//excluding and Pp[i] the same situation
                   TEMP+=NUM1[T]; else temp+=num1[t]-1;
           Remove one and pp[i] equality of a situation} else temp+=num1[t];
       else break; } ANS+=TEMP/3;
           Calculated each case three times//There are two identical, another one is not the same for (int i=1;i<=cnt;i++) {int t=x-pp[i]*2;
                        if (t>=2) {if (Prime[t]) {if (t!=pp[i))
               ans++;
       } else break; ///three identical for (int i=1;i<=cnt;i++) {if (x>=pp[i]*3) {if (x==pp[i]*3) ans+
           +;
       else break;
       }///Two number addition//two different number of additions ANS+=NUM1[X];
       two identical ans+=num2[x];

   printf ("%lld\n", ans);
return 0;
 }



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.