我是靠谱客的博主 大力悟空,这篇文章主要介绍HDU 5587 Array,现在分享给大家,希望可以做个参考。

Problem Description

Vicky is a magician who loves math. She has great power in copying and creating. One day she gets an array {1}。 After that, every day she copies all the numbers in the arrays she has, and puts them into the tail of the array, with a signle '0' to separat. Vicky wants to make difference. So every number which is made today (include the 0) will be plused by one. Vicky wonders after 100 days, what is the sum of the first M numbers.

Input

There are multiple test cases. First line contains a single integer T, means the number of test cases.left( 1 leq T leq 2 * {10}^{3} right)(1T2103) Next T line contains, each line contains one interger M. left( 1leq M leq {10}^{16} right)(1M1016)

Output

For each test case,output the answer in a line.

Sample Input
3
1
3
5
Sample Output
1
4

7

转换为2进制做类似数位dp就好了

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
int n;
long long m;
long long a[maxn], b[maxn];

void init()
{
    for (int i = a[b[0] = 0] = 1; i <= 100; i++) a[i] = a[i - 1] << 1;
    for (int i = 1; i <= 100; i++) b[i] = (b[i - 1] << 1) + a[i - 1];
    for (int i = 0; i <= 100; i++) a[i]--;
}

long long solve(LL x,LL y)
{
    for (int i = 0; i <= 100; i++) 
    if (x > a[i] && x <= a[i + 1])
    {
        if (x == a[i + 1])    return b[i + 1] + y*a[i + 1];
        else return b[i] + y*(a[i] + 1) + 1 + solve(x - a[i] - 1, y + 1);
    }
}

int main()
{
    init();
    while (scanf("%d", &n) != EOF)
    {
        while (n--)
        {
            scanf("%I64d", &m);
            printf("%I64dn", solve(m, 0));
        }
    }
    return 0;
}


最后

以上就是大力悟空最近收集整理的关于HDU 5587 Array的全部内容,更多相关HDU内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部