SDNU 1537.Tiger Eat People

本文详细解析了一道ACM竞赛中常见的子集求和问题,通过实例输入输出展示了如何计算给定数量的数字能以多少种方式相加得到的目标数。代码采用C++实现,利用位运算技巧高效解决了大规模数据处理问题。

Description

As we all know, lls is the boss of SDNU ACM Training Team, so many team members may be afraid of him.
But this is not the case, lls is very kindly to us. To show his love to us, he plan to give all of us an "Accepted". The problem shows as follows:
There are n numbers ​.Each time you can choose a subset of them(may be empty), and then add them up.Count how many numbers can be represented in this way.

Input

The first line of the input contains an integer T, denoting the number of test cases.
In each test case, there is a single integers n in one line, as described above.
,

Output

For each test case, output one line contains a single integer, denoting the answer.

Sample Input

4
9
7
8
233

Sample Output

512
128
256
13803492693581127574869511724554050904902217944340773110325048447598592
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#include <string>
#include <cmath>

using namespace std;

#define ll long long
#define maxn 1000000+8

int bit[maxn];

int main()
{
    int t;
    scanf("%d", &t);
    for(int i = 0; i < t; i++)
    {
        int n;
        scanf("%d", &n);
        int l = 0;
        bit[0] = 1;//相乘先把第一个数弄成0
        for(int i = 1; i <= n; i++)
        {
            int a = 0;
            for(int j = 0; j<= l;j++)
            {
                bit[j] = bit[j]*2+a;//计算2的次方
                a = 0;
                if(bit[j] >= 10)
                {
                    bit[j] -= 10;//将10位进到下一个
                    a++;
                }
            }
            if(a)//如果需要进位
                {
                    l++;
                    bit[l] = 1;//那这个大数据就向前进一位
                }
        }
        for(int i = l; i >= 0; i--)
        {
            printf("%d", bit[i]);
            bit[i] = 0;
        }
        printf("\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/RootVount/p/10328506.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值