hdu-5241

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(T≤20T≤20) denoting the number of test cases.

For each test case, the first line contains an integer nn(0≤n≤30000≤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
2
0
2
Sample Output
Case #1: 1
Case #2: 1024

解题思路:答案为32的n次方,我也不知道为什么是这个答案,我是看别人的题解才知道的,据说是看出来,我只想说6666.
通过这题我总结了乘法大数算法,令d[i]为一个大数的第i位,从右开始,第一位(个位)为0位,则有大数a,b,的乘积为大数c,c.d[i + j] = a.d[i]*b.d[j];但是这样之后c的每一位可能大于9,所以需要对每一位进行取余(与10),并且向高位进行进位,直到a.len + b.len。最后从最高位(a.len + b.len)开始向低位判断,c到底有多少位,注意在重载^运算符时,不要一个一个乘,要用快速幂思想,不然这一题会超时。

#include
using namespace std;
const int maxn = 1e5;
class bignum{
public:
    int d[maxn];//每一位
    int len;//数的长度
    bignum()
    {
        memset(d,0,sizeof(d));
    }
    bignum(int x)
    {
        int l = 0;
        memset(d,0,sizeof(d));
        while(x)
        {
            d[l++] = x%10;
            x /= 10;
        }
        len = l;
    }
    bignum operator * (const bignum &res) const
    {
        bignum ans;
        for(int i = 0; i < this->len; i++)
        {
            for(int j = 0; j < res.len; j++)
            {
                ans.d[i + j] += d[i]*res.d[j];
            }
        }
        for(int i = 0; i < this->len + res.len; i++)
        {
            ans.d[i + 1] += ans.d[i]/10;
            ans.d[i] %= 10;
        }
        int Len  = this->len + res.len;
        while(Len > 1&&ans.d[Len - 1] == 0)
        {
            Len--;
        }
        ans.len = Len;
        return ans;
    }
    bignum operator ^ (const int x) const
    {
        bignum ans(1);
        bignum res;
        res.len = this->len;
        int term = x;
        for(int i = 0; i < this->len; i++)
        {
            res.d[i] = this->d[i];
        }
        while(term)
        {
            if(term&1) ans = ans*res;
            term >>= 1;
            res = res*res;
        }
        return ans;

    }
    void print()
    {
        for(int i = len - 1; i >= 0; i--)
        {
            printf("%d",d[i]);
        }
        printf("\n");
    }
};
int main()
{
    int T;
    int Case = 1;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        bignum term(32);
        term = term^n;
        printf("Case #%d: ",Case++);
        term.print();
    }
    return 0;
}

你可能感兴趣的:(高精度)