hdu 5587 Array 2015.11.28 bestcoder 1003

来源:互联网 发布:sql日期格式转换成年日 编辑:程序博客网 时间:2024/06/07 23:36

题意:一开始是一个数列1,每操作一次就复制一遍这个数列放到后面,然后在他们之间放一个0,然后从这个0开始包括0后面每一个数都+1。1——112——1121223……然后这个数列经过若干次操作,问你前M个数的合是多少,M最大是10的16次方

思路:我们发现每一次操作之后,他们的和都是上一个状态的和*2+2的n-1次方,n代表第几个数列。于是打个表A,大小最多五六十。然后我们发现每一个数列的长度都是2的n次方-1。于是我又打了个表B记录这些长度。最后,给你一个M,你就对它dfs,在表B里用二分寻找小于等于它的第一个数的序号,然后对应的A数组里存的就是这前若干项的和。把这个和加到最终解里,对于剩下的一部分长度为 L 的序列,因为题意说后面要全+1所以我们要+L,然后还说中间间隔一个0所以再加dfs(L-1)。递归一遍,很快的。我估计找B的位置的时候不用2分都ok,因为表B也很小,就几十。



Array

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


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
3135
 

Sample Output
147
 

Source
BestCoder Round #64 (div.2)
 

#include <iostream>#include <stdio.h>#include <math.h>#include <stdlib.h>#include <string>#include <string.h>#include <algorithm>#include <vector>#include <queue>#include <iomanip>#include <time.h>#include <set>#include <map>#include <stack>using namespace std;typedef long long LL;const int INF=0x7fffffff;const int MAX_N=10000;int T;long long M;long long A[109];long long B[109];long long dfs(long long x){    long long ans=0;    long long b=upper_bound(B+1,B+63,x)-upper_bound(B+1,B+63,1)+1;    ans+=A[b];    if(x-B[b]==0)return ans;    if(x-B[b]==1)return ans+1;    ans+=dfs(x-B[b]-1)+x-B[b];    return ans;}int main(){    A[1]=1;    long long m=1;    for(int i=2;i<=100;i++){        A[i]=A[i-1]*2+2*m;        m*=2;    }    B[1]=1;    for(int i=2;i<=100;i++){        B[i]=2*B[i-1]+1;    }    cin>>T;    while(T--){        scanf("%I64d",&M);        long long ans=0;        printf("%I64d\n",dfs(M));    }    return 0;}



1 0
原创粉丝点击