hdu 5587 Array 2015.11.28 bestcoder 1003

题意:一开始是一个数列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
  
  
3 1 3 5
 

Sample Output
  
  
1 4 7
 

Source
 

#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
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值