JZ junior OJ 1564. [GDKOI] tourism

Title Description

    Number of inputs N, select some of the calculated sum up, ask how many such method is selected and a prime number.
 

Entry

A first line integer N.

The second line of N integers, N represents the number of values ​​of these.

Export

    An integer representing the number of programs.
 

Sample input

4
1 1 2 7

Sample Output

5
 

Data range limit

 
 

prompt

Sample [explain]

   A total of 12 kinds of method selected from: (1,1,2,7), (1,2,7), (2,7), (1,1,7), (1,7), (7), ( 1,1,2), (1,2), (2), (1,1), (1) and (), wherein the (1,1,2,7), (7), (1,2) , (1,1), (2) is selected from five kinds of the correct method.

【limit】

1<=N<=50。

Each number does not exceed 10,000.
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct s
 4 {
 5     long long b;
 6     int g;
 7 }a[100];
 8 bool cmd(s x,s y)
 9 {
10     return x.b<x.b;
11 }
12 bool zs[600000],sf[10001];
13 long long n,all,x,ans,maxn,f[510000];
14 void zhishu()
15 {
16     for(int i=2;i<=600000;i++)
17     {
18         if(zs[i]==0)
19         {
20             int l=2;
21             while(i*l<=600000)
22             {
23                 zs[i*l]=1;
24                 l++;
25             }
26         }
27     }
28     zs[1]=1;
29     zs[0]=1;
30 }
31 int main()
32 {
33     zhishu();
34     cin>>n;
35     for(int i=1;i<=n;i++)
36     {
37         cin>>x;
38         if(sf[x]==0)
39         {
40             a[++all].g=1;
41             a[all].b=x;
42             sf[x]=1;
43         }
44         else
45             for(int j=1;j<=all;j++)
46                 if(a[j].b==x)
47                     a[j].g++;
48     }
49     f[0]=1;
50     sort(a+1,a+1+all,cmd);
51     for(int i=1;i<=all;i++)
52     {
53         for(int j=maxn;j>=0;j--)
54         {
55             for(int k=1;k<=a[i].g;k++)
56             {
57                 f[j+a[i].b*k]+=f[j];
58             }
59         }
60         maxn+=a[i].b*a[i].g;
61     }
62     for(int i=1;i<=maxn;i++)
63     {
64         if(zs[i]==0)
65             years + = f [i];
66      }
 67      cout << years;
68      return  0 ;
69 }

Guess you like

Origin www.cnblogs.com/anbujingying/p/11317193.html
Recommended