Friends (HDU

来源:互联网 发布:百度知道 知乎 编辑:程序博客网 时间:2024/06/07 06:58

Mike has many friends. Here are nine of them: Alice, Bob, Carol, Dave, Eve, Frank, Gloria, Henry and Irene. 

Mike is so skillful that he can master nn languages (aka. programming languages). 

His nine friends are all weaker than he. The sets they can master are all subsets of Mike's languages. 

But the relations between the nine friends is very complex. Here are some clues. 

1. Alice is a nice girl, so her subset is a superset of Bob's. 
2. Bob is a naughty boy, so his subset is a superset of Carol's. 
3. Dave is a handsome boy, so his subset is a superset of Eve's. 
4. Eve is an evil girl, so her subset is a superset of Frank's. 
5. Gloria is a cute girl, so her subset is a superset of Henry's. 
6. Henry is a tall boy, so his subset is a superset of Irene's. 
7. Alice is a nice girl, so her subset is a superset of Eve's. 
8. Eve is an evil girl, so her subset is a superset of Carol's. 
9. Dave is a handsome boy, so his subset is a superset of Gloria's. 
10. Gloria is a cute girl, so her subset is a superset of Frank's. 
11. Gloria is a cute girl, so her subset is a superset of Bob's. 

Now Mike wants to know, how many situations there might be. 
Input
The first line contains an integer TT(T20T≤20) denoting the number of test cases. 

For each test case, the first line contains an integer nn(0n30000≤n≤3000), denoting the number of languages. 

Output
For each test case, output ''Case #t:'' to represent this is the t-th case. And then output the answer. 
Sample Input
202
Sample Output
Case #1: 1Case #2: 1024

题目就是让我们求一共有多少种情况;

解题思路:
本人实在不知道大神们是怎么推出32的n次方的,模拟赛的时候蒙了一波,没想到猜对了,实现方法却超时了,这里用到了大数的幂运算。

这里才知道自己为什么超时,数组存储的位数越多(当然是在合理的情况下),越省时间。

详见代码:

#include <cstdio>#include <iostream>#include <cstring>using namespace std;int b[10000000];int main(){int  t;int a;int len;scanf("%d",&t);for(int i=1;i<=t;i++){scanf("%d",&a);len = 1;memset(b,0,sizeof(b));b[1] = 1;for(int k=1;k<=a;k++){int c = 0;for(int j=1;j<=len;j++){   b[j]*= 32;  // 这里每个数组存储的是个两位数,不清楚的话可以手动模拟一下   b[j] += c;   c = b[j]/100;   b[j] %= 100;} while(c!=0){       b[++len] = c%100;          c /= 100;}         }    printf("Case #%d: ",i);     for(int w=len w>=1; w--)    {  if(w!=len) printf("%02d",b[w]); // 输出的时候要特别注意,因为b[i]存储的是个两位数,输出的时候
                                                  // 不足两位要补零,不然b[i]表示的意义就变了。  else printf("%d", b[w]);     } printf("\n");}    return 0;} 


-------------------------------------------------------------------------------------------------------------------------------------------------

附上超时代码:

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int a[10000000];int b[10000000];int aa[10000000];int bb[10000000];int er[100];int erjin[100];int san[100];int jinsan[100];int main(){    int T;    int f;    for(int i=0;i<35;i++)    {        er[i] = i%10;        erjin[i] = i/10;    }    int ai,bi,ci;    scanf("%d",&T);    for(int  i=1; i<=T; i++)    {       scanf("%d",&f);       memset(a,0,sizeof(a));       memset(b,0,sizeof(b));       memset(aa, 0, sizeof(aa));       memset(bb, 0, sizeof(bb));       a[1]=3;       a[2]=2;       int num =0;       int sum =0;       if(f==1){cout << "32\n";continue;}       if(f==0){cout << "1\n";continue;}       ai = 2;       for(int w=2;w<=f;w++)       {         ai++;         num = 0;         for(int t = ai-1;t>=0 ;t--)         {             aa[t] =  a[t]*2 + num;             if(aa[t] >=10) {num = erjin[aa[t]]; aa[t] = er[aa[t]];}             else num = 0;         }         /*for(int r=0;r<ai;r++)            cout << aa[r];            cout << endl;*/         num = 0;         for(int t = ai-1 ;t>=0; t--)         {             bb[t]= a[t] * 3+ num;             if(bb[t] >= 10){num = erjin[bb[t]]; bb[t] = er[bb[t]];}             else num = 0;         }         /*for(int r=0;r<ai;r++)            cout << bb[r];            cout << endl;*/         num = 0;         for(int j=ai; j>=1; j--)         {            b[j] = aa[j-1]+bb[j] + num;            if(b[j]>=10){num = erjin[b[j]];b[j] = er[b[j]];}            else num = 0;         }         b[0] = num;         memcpy(a+1,b,sizeof(b));         a[0] = 0;         ai++;         //a[ai+1] = 0;       }       int flag=1;       for(int t=1;t<=ai;t++)       {          if(a[t]==0&&flag==1)continue;          flag = 0;          printf("%d",a[t]);       }       printf("\n");     }     return 0;}

水波。

有木有大佬可以解释一下为什么是2^n^5?

0 0