HDU 5587 Array

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587

Array

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 339    Accepted Submission(s): 167


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.(1T2103)
Next T line contains, each line contains one interger M. (1M1016)
 

 

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

 

Sample Input
3 1 3 5
 

 

Sample Output
1 4 7
 

 

Source
 

题目大意:输入数字M,输出前M项和。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stdlib.h>

using namespace std;

long long sum[1000];
long long num[1000];

void Init()
{
    sum[1]=1;
    num[1]=1;
    for(int i=2;i<=60;i++)
    {
        num[i]=num[i-1]*2+1;
        sum[i]=sum[i-1]*2+num[i-1]+1;
    }

}

long long Find(long long M)
{
    if(M<=0)
        return 0;
    int k=lower_bound(num+1,num+55,M)-num;

    if(num[k]==M)
        return sum[k];
    else
        return Find(M-num[k-1]-1)+sum[k-1]+M-num[k-1];

}

int main()
{
    int t;
    long long M;
    Init();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&M);
        long long ans=Find(M);
        printf("%lld\n",ans);
    }

    return 0;
}

 

posted @ 2015-11-30 15:11  梦中。。  阅读(122)  评论(0编辑  收藏  举报