BestCoder Round #64 (div.2) HDOJ5587 Array(dfs)

Vicky是一位热爱数学的魔法师,她通过复制和创建数组来施展魔法。此题描述了一个特殊的数组操作过程,并提供了解决方案及代码实现,涉及数组处理、二进制运算等算法技巧。

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

Array

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


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
 



题目链接:点击打开链接

a[i]为i的二进制表示下1的个数, 预处理得到f, g数组, 对每次读入的m进行dfs即可.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 105;
ll m, f[MAXN], g[MAXN];
void magic()
{
    f[1] = 1, g[1] = 1;
    for(int i = 2; i <= 63; ++i)
        f[i] = f[i - 1] * 2 + 1;
    for(int i = 2; i <= 63; ++i)
        g[i] = g[i - 1] * 2 + f[i - 1] + 1;
}
ll dfs(ll x)
{
    if(x <= 1) return x;
    int y = lower_bound(f + 1, f + 63, x) - f;
    if(f[y] == x) return g[y];
    return g[y - 1] + dfs(x - f[y - 1] - 1) - f[y - 1] + x;
}
int main(int argc, char const *argv[])
{
    magic();
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%lld", &m);
        printf("%lld\n", dfs(m));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值