• HDU 5241 Friends(大数+快速幂)


    Friends

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
    Total Submission(s): 1356    Accepted Submission(s): 639


    Problem Description
    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 n 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 T(T20) denoting the number of test cases.

    For each test case, the first line contains an integer n(0n3000), 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
    2 0 2
     

    Sample Output
    Case #1: 1 Case #2: 1024
     

    Source
     

    Recommend


    题意 :(原帖here) 
    Mike 有很多朋友,他们的名字叫Alice, Bob, Carol, Dave, Eve, Frank, Gloria, Henry and Irene. 
    Mike会n种语言, 他的朋友会的语言数目都是Mike的子集 
    并且他的朋友有以下关系: 
    1.Alice 是Bob 的子集 
    2.Bob 是Carol 的子集 
    3.Dave 是Eve 的子集 
    4.Eve 是Frank 的子集 
    5.Gloria是Henry的子集 
    6.Henry是Irene 的子集 
    7.Alice 是Eve 的子集 
    8.Eve 是Carol 的子集 
    9.Dave 是Gloria的子集 
    10.Gloria是Frank的子集 
    11.Gloria是Bob 的子集 
    现在Mike想知道有多少种可能?

    这道题看输入输出第一想法就是把N为1的输出手推出来,然后推了两次都没推对 T.T。

    对于每一种语言来说,这种语言谁会谁不会是的方案数一定,而且语言之间相互独立的,互相之间通过乘法原理来计算方案数,这样就一定是某一个数的n次方 
    通过样例看出是32的n次方



    我只想说做的时候真心没想到竟然是这么一道“有内涵”的题目,还是被学弟学妹手把手教会的,真心没想到。。。

    之前的模板都乱了,今天发现之前整理的这个还是挺好的!!!here

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    using namespace std;
    
    #define MAXN 9999//万进制
    #define DLEN 4//4位
    
    class BigNum
    {
    private:
        int a[40000];///可以控制大数位数(500*4)
        int len;///大数长度
    public:
        BigNum() ///构造函数
        {
            len=1;
            memset(a,0,sizeof(a));
        }
        BigNum(const int b) ///将int转化为大数
        {
            int c,d=b;
            len=0;
            memset(a,0,sizeof(a));
            while(d>MAXN)
            {
                ///c=d-(d/(MAXN+1))*(MAXN+1);
                c=d%(MAXN+1);///取出后四位
                d=d/(MAXN+1);//
                a[len++]=c;
            }
            a[len++]=d;
        }
        BigNum operator*(const BigNum &T)const //大数*大数
        {
            BigNum ret;
            int i,j,up;
            int temp,temp1;
            for(i=0; i<len; ++i)
            {
                up=0;
                for(j=0; j<T.len; ++j)
                {
                    temp=a[i]*T.a[j]+ret.a[i+j]+up;
                    if(temp>MAXN)
                    {
                        //temp1=temp-temp/(MAXN+1)*(MAXN+1);
                        temp1=temp%(MAXN+1);
                        up=temp/(MAXN+1);
                        ret.a[i+j]=temp1;
                    }
                    else
                    {
                        up=0;
                        ret.a[i+j]=temp;
                    }
                }
                if(up!=0)ret.a[i+j]=up;
            }
            ret.len=i+j;
            while(ret.a[ret.len-1]==0&&ret.len>1)--ret.len;
            return ret;
        }
    
        void print() //输出大数
        {
            int i;
            printf("%d",a[len-1]);
            for(i=len-2; i>=0; --i)
            {
                printf("%.4d",a[i]);///%.4d代表4位,不够前面补0
            }
            printf("
    ");
        }
    };
    
    int main()
    {
        //freopen("out.txt","w",stdout);
        int n,T,Case=1;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            BigNum a(32),ans(1);
            while(n>0)
            {
                if(n&1) 
                    ans=ans*a; 
                if(n==0) break;
                a=a*a;
                n=n>>1; 
            }
            printf("Case #%d: ",Case++);
            ans.print();
        }
        return 0;
    }
    



    Java都忘差不多了,不过代码相当清爽!!!

    import java.util.*;
    import java.math.*;
    
    public class Main
    {
        static public void main(String[]args)
        {
            Scanner cin = new Scanner(System.in);
            int T = cin.nextInt();
            for (int i = 1; i <= T; i++)
            {
                int n = cin.nextInt();
                System.out.printf("Case #%d: ", i);
                System.out.println(BigInteger.valueOf(32).pow(n));
            }
    
        }
    }


  • 相关阅读:
    KindEditor限制输入字数
    datepicker日历控件使用
    复合注解的解析
    jdk包结构及用途分析
    深入理解BIO、NIO、AIO
    MiniCat:手写Http服务器
    基于TCP协议的Socket编程
    手写一个最迷你的Web服务器
    手写一个Web服务器,极简版Tomcat
    java网络编程socket的使用
  • 原文地址:https://www.cnblogs.com/zswbky/p/6792861.html
Copyright © 2020-2023  润新知