hdoj Array 5587 (机智打表)

211 篇文章 1 订阅
40 篇文章 1 订阅

Array

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


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
 
//中文题
 

Array

Accepts: 112
Submissions: 324
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)
问题描述
Vicky是个热爱数学的魔法师,拥有复制创造的能力。
一开始他拥有一个数列{1}。每过一天,他将他当天的数列复制一遍,放在数列尾,并在两个数列间用0隔开。Vicky想做些改变,于是他将当天新产生的所有数字(包括0)全加1。Vicky现在想考考你,经过100天后,这个数列的前M项和是多少?。
输入描述
输入有多组数据。
第一行包含一个整数T,表示数据组数。T. (1≤T≤2∗103)\left( 1 \leq T \leq 2 * {10}^{3} \right)(1T2103)
每组数据第一行包含一个整数M. (1≤M≤1016)\left( 1\leq M \leq {10}^{16} \right)(1M1016)
输出描述
对于每组数据输出一行答案.
输入样例
3
1
3
5
输出样例
1
4
7
Hint
第一项永远为数字111,因此样例1输出111
第二天先复制一次,用0隔开,得到{1,0,1},再把产生的数字加1,得到{1,1,2},因此样例2输出前3项和1+1+2=41+1+2=41+1+2=4.
第三天先得到{1,1,2,0,1,1,2},然后得到{1,1,2,1,2,2,3},因此样例3输出前5项和1+1+2+1+2=71+1+2+1+2=71+1+2+1+2=7
 
#include<stdio.h>
int main()
{
    long long a[55]={0,1,3,7,15};
    long long sum[55]={0,1,4,};
    for(int i=2;i<55;i++)
    {
        a[i]=a[i-1]*2+1;     ///个数
        sum[i]=sum[i-1]*2+1+a[i-1];   ///和
    }
    int T,i;
    long long n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        long long ans=0;
        while(n>0)
        {
            for(i=54;i>=1;i--)
            	if(n>=a[i]) 
					break;
            n-=a[i];
            ans=ans+sum[i]+n;
            n--;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值