BestCoder Round #64 (div.2) 1003 Array HDU 5587

本文深入探讨了信息技术领域的核心内容,包括但不限于前端开发、后端开发、移动开发、游戏开发、大数据开发、开发工具等多个细分领域。文章通过具体实例和代码示例,详细讲解了每个领域的关键技术和应用,旨在帮助读者全面掌握信息技术领域的基础知识和实战技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Array

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


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

出题人:

其实{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,其高位与低位的种数即可。


我用搜索

先求出1,3,7,15。。。。。的前缀和( a[i] )及对应位置 (b[i] )

输入数n 

再搜索3中情况

1 b[i]=n     return  a[i] 

2 b[i]+1=n   return  a[i]+1

3 b[i]+1<n return  a[i]+dfs(n-b[i]-1)+num-b[i];


#include
  
   
#include
   
    
#include
    
     
#include
     
      
#include
      
using namespace std;
const int N=100005;
long long a[N]= {0};    ///前缀和
long long b[N];         ///前缀和对应的位置
long long n,m,t;
int kkk=0;
int fun(long long num)  ///num对应的位置
{
    for(; t>=1; t--)
    {
        if(b[t]<=num) return t;
    }
}
long long dfs(long long num)
{
    kkk++;
    int temp=fun(num);
    if(b[temp]==num) return a[temp];    ///1
    else if(b[temp]+1==num) return a[temp]+1;   ///2
    else return a[temp]+dfs(num-b[temp]-1)+num-b[temp]; ///3
}
int main()
{
    int T,i,kk;
    for(i=1; i<60; i++)
    {
        b[i]=b[i-1]*2+1;
        a[i]=b[i-1]+1+a[i-1]*2;
        if(b[i]>10000000000000000) break;
    }
    scanf("%d",&T);
    while(T--)
    {
        t=54;
        scanf("%lld",&n);
        printf("%lld\n",dfs(n));
    }
    return 0;
}

     
    
   
  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值