HDU 5587 Array(位运算)——BestCoder Round #64(div.1 div.2)

Array

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


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
 

/************************************************************************/

附上该题对应的中文题

Array

 
 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. \left( 1 \leq T \leq 2 * {10}^{3} \right)(1T2103)
每组数据第一行包含一个整数M. \left( 1\leq M \leq {10}^{16} \right)(1M1016)
输出描述
对于每组数据输出一行答案.
输入样例
3
1
3
5
输出样例
1
4
7
Hint
第一项永远为数字11,因此样例1输出11
第二天先复制一次,用0隔开,得到{1,0,1},再把产生的数字加1,得到{1,1,2},因此样例2输出前3项和1+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=7

/****************************************************/

出题人的解题思路:

Array

其实{A}_{i}Ai为i二进制中1的个数。每次变化{A}_{k+{2}^{i}}={A}_{k}+1Ak+2i=Ak+1 ,(k<{2}^{i})(k<2i)不产生进位,二进制1的个数加1。然后数位dp统计前m个数二进制1的个数,计算每一位对答案的贡献。只需考虑该位填1,其高位与低位的种数即可。

首先,我们先来看看100天后该数列前面的一些项:

1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,……

起初,我还在想怎么找规律啊什么的,后来突然发现了其中的猫腻

第0项: 0……0000   0个1 (该项无实际意义,但可简化求解过程)

第1项: 0……0001   1个1

第2项: 0……0010   1个1

第3项: 0……0011   2个1

第4项: 0……0100   1个1

第5项: 0……0101   2个1

第6项: 0……0110   2个1 

第7项: 0……0111   3个1

...

...

...

而我们要求的前m项和,其实就是前m项1~m的二进制数表示的1的总个数

另外,我们可以发现,二进制数从低位到高位,对于第i位,01交替都是以为周期,那我们只要看有多少个这样的周期,以及最后一个不满一个周期里有多少个1就是我们要求解的

即对于第i位,我们只需求出周期数和最后一个不满一个周期里有多少个数

因为为周期的位,有一半是1,一半是0,所以一个周期会贡献个1

而最后一个周期,如果它的个数超过周期数的一半,它才会有所贡献,因为一个周期里前一半都是0,而后一半才全是1

故最终我们可以得到如下代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 100005;
const int M = 10005;
const int inf = 1000000000;
const int mod = 10007;
int main()
{
    int i,t;
    __int64 m,a,b,sum,k,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d",&m);
        for(sum=0,j=1,i=1;i<55;i++)
        {
            k=j<<i;//printf("##%I64d ",k);
            a=m/k;
            b=m%k;
            //printf("%I64d %I64d\n",a,b);
            if(b+1>k/2)
                sum=sum+a*k/2+b-k/2+1;
            else
                sum=sum+a*k/2;
        }
        printf("%I64d\n",sum);
    }
    return 0;
}
菜鸟成长记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值