hdu 5587 Array 数学题

Array

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5587

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.(1≤T≤2∗103)
Next T line contains, each line contains one interger M. (1≤M≤1016)

Output

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

Sample Input

3 1 3 5

Sample Output

1 4 7

HINT

题意

hdu 5587 Array 数学题

题解:

数学题(:

首先我们先用找规律算出第i天一共能得到的和是多少:g(i)=g(i-1)+2^(i-1)

然后我们就可以递归求解了,不断地利用2的幂来进行递归

代码:

#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std; vector<long long>Q;
long long ans = ;
long long g[];
void solve(long long k)
{
if(k<=)return;
int p = --upper_bound(Q.begin(),Q.end(),k)-Q.begin();
ans+=g[p]+k-Q[p];
solve(k-Q[p]-);
}
int main()
{ long long T=;
Q.push_back();
for(int i=;i<=;i++)
{
Q.push_back(T-);//2^i-1
T*=;
}
g[]=;long long tmp=;
for(int i=;i<=Q.size();i++)
{
g[i]=*g[i-]+tmp;//存的是第i天的答案
tmp=tmp*;
}
int t;scanf("%d",&t);
for(int i=;i<=t;i++)
{
long long x;scanf("%I64d",&x);
ans = ;
solve(x);
printf("%I64d\n",ans);
}
}
上一篇:北大poj- 1013


下一篇:深入理解JVM(4)——对象内存的分配策略