第一次个人赛

4个小时只A了一题,太水了,开始做的F题,一看母函数的题啊,这么水,立马敲,敲了之后交了WA,太坑了这题前3个小时都没人出这题

天坑,后来知道暴力枚举就能过。

Coin Change

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10572    Accepted Submission(s): 3522


Problem Description
Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.

For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.

Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 100 coins.
 

 

Input
The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.
 

 

Output
For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.
 

 

Sample Input
11 26
 

 

Sample Output
4 13
 

 

Author
Lily
母函数的话没懂这句话“Your program should be able to handle up to 100 coins."也就是说5种硬币的数目不能超过一百,英语是硬伤,
贴上一份错误的母函数代码
 1 #include<stdio.h>
 2 __int64  c1[100000],c2[100000];
 3 int main()
 4 {
 5    __int64  n,i,j,k,t,a[6]={1,5,10,25,50};
 6      while(scanf("%I64d",&n)!=EOF)
 7      {
 8           //if(n==0)
 9           //{
10              //  printf("0\n");
11              //  continue;
12          // }
13           for(i=0;i<=n;i++)
14           {
15                c1[i]=0;
16                c2[i]=1;
17           }
18           for(i=1;i<=5;i++)
19           {
20                for(j=0;j<=n;j++)
21                {
22                     for(k=0;k+j<=n;k+=a[i-1])
23                     {
24                          c2[j+k]+=c1[j];
25                     }
26                }
27                for(t=0;t<=n;t++)
28                {
29                     c1[t]=c2[t];
30                     c2[t]=0;
31                }
32           }
33           printf("%I64d\n",c1[n]);
34 
35      }
36      return 0;
37 }
View Code

 

 比赛是在晚上搞得,一直到12点,只A了一题心情不爽,睡了,第二天想了一下暴力枚举的方法
太痛苦了,竟然超时了,贴上一份超时代码。
 1 #include<stdio.h>
 2 int main()
 3 {
 4      int i,j,k,t,n,ans,p;
 5     // freopen("in.txt","r",stdin);
 6     // freopen("out.txt","w",stdout);
 7      while(scanf("%d",&n)!=EOF)
 8      {
 9           ans=0;
10       
11           for(i=0;i<=n;i++)
12           {
13                for(j=0;j<=n/5;j++)
14                {
15                   
16                     for(k=0;k<=n/10;k++)
17                     {
18 
19                        for(t=0;t<=n/25;t++)
20                          {
21                               
22                               for(p=0;p<=n/50;p++)
23                               {
24                                    if((i+j+k+t+p)<=100&&(i+5*j+k*10+t*25+p*50)==n)
25                                   ans++;
26                               
27                               
28                               }
29 
30 
31 
32 
33                          }
34 
35                     }
36 
37                }
38           }
39 
40 
41 
42                    printf("%d\n",ans);
43 
44      }
45 
46      return 0;
47 }
View Code

后来实在是火了,直接打表。AC的打表的代码

 1 #include<stdio.h>
 2 int main()
 3 {
 4 
 5 int n;
 6 
 7 int a[300]={1,1,1,1,1,2,2,2,2,2,4,4,4,4,4,6,6,6,6,6,9,9,9,9,9,13,13
 8 ,13,13,13,18,18,18,18,18,24,24,24,24,24,31,31,31,31,31,39
 9 ,39,39,39,39,50,50,50,50,50,62,62,62,62,62,77,77,77,77,77,93,
10 93,93,93,93,112,112,112,112,112,134,134,134,134,134,159,159,159
11 ,159,159,187,187,187,187,187,218,218,218,218,218,252,252,252,252,
12 252,292,291,291,291,291,333,333,333,333,332,380,380,380,379,378,430,
13 430,429,428,427,485,484,483,482,482,544,543,542,541,539,608,607,606,604,
14 602,677,676,673,671,670,751,748,746,744,743,828,825,823,822,818,912,910,908,
15 904,900,1001,999,995,990,986,1098,1093,1088,1084,1081,1196,1191,1186,1182,1177,
16 1301,1296,1292,1285,1278,1413,1408,1400,1393,1387,1532,1524,1515,1508,1503,1654,
17 1644,1637,1631,1622,1782,1773,1766,1757,1746,1916,1909,1898,1886,1875,2061,2049,
18 2037,2025,2015,2208,2194,2181,2170,2156,2361,2348,2336,2321,2306,2521,2508,2492,
19 2475,2459,2691,2673,2654,2637,2622,2863,2843,2824,2808,2789,3042,3021,3004,2983,
20 2960,3228,3209,3187,3164,3140,3424,3401,3376,3351,3329,3623,3596,3570,3545,3517,
21 3830,};
22 while(scanf("%d",&n)!=EOF)
23 {
24      printf("%d\n",a[n]);
25 }
26 return 0;
27 }
View Code

 

后来看了一下别人AC的暴力枚举的代码,想想是自己的那个变量没处理的好
也贴上一份
 1 #include<stdio.h>
 2 int main()
 3 {
 4      int i,j,k,t,n,ans,p;
 5     // freopen("in.txt","r",stdin);
 6     // freopen("out.txt","w",stdout);
 7      while(scanf("%d",&n)!=EOF)
 8      {
 9           ans=0;
10 
11           for(i=0;5*i<=n;i++)
12           {
13                for(j=0;10*j<=n-5*i;j++)
14                {
15                     //printf("%d %d\n",j,n/5);
16                     for(k=0;25*k<=n-5*i-10*j;k++)
17                     {
18 
19                        for(t=0;50*t<=n-5*i-10*j-25*k;t++)
20                          {
21                              int p=n-5*i-10*j-25*k-50*t;
22 
23                                    if(p>=0&&((i+j+k+t+p)<=100))
24                                   ans++;
25 
26 
27 
28 
29 
30 
31                          }
32 
33                     }
34 
35                }
36           }
37 
38 
39 
40                    printf("%d\n",ans);
41 
42      }
43 
44      return 0;
45 }
View Code

不过网上还有用DP做的,不过现在还不怎么懂暂时不贴了。

 下午渊哥讲了一下这题,用母函数竟然可以
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <map>
 5 #include <set>
 6 #include <vector>
 7 #include <algorithm>
 8 #include <cmath>
 9 #include <queue>
10 #include <stack>
11 using namespace std;
12 #define Min(a,b) (a < b ? a : b)
13 #define Max(a,b) (a > b ? a : b)
14 #define mem(f,val) memset(f,val,sizeof(f))
15 #define INF 0x7FFFFFFF
16 #define M 100
17 int c1[300][200];
18 int c2[300][200];
19 int data[7]={0,1,5,10,25,50};
20 void solve()
21 {
22     c1[0][0] = 1;
23     for(int i = 1; i <= 5; i++)
24     {
25         for(int j = 0; j <= 250; j++)
26         {
27             for(int k = 0; data[i] * k + j <= 250; k++)
28             {
29                 for(int t = 0; t + k <= 100; t++)
30                 {
31                     c2[data[i] * k + j][t + k] += c1[j][t];
32                 }
33             }
34         }
35         memcpy(c1,c2,sizeof(c2));
36         mem(c2,0);
37     }
38 }
39 int main()
40 {
41     int n;
42     solve();
43     while(scanf("%d",&n)!=EOF)
44     {
45         int ans = 0;
46         for(int i = 0; i <= 100; i++)
47         {
48             ans += c1[n][i];
49         }
50         printf("%d
51 ",ans);
52     }
53     return 0;
54 }
View Code

 

posted @ 2013-07-21 11:08  SprayT  阅读(219)  评论(0编辑  收藏  举报