light oj 1032(数位dp)

本文介绍了一道关于二进制中相邻1计数的问题——FastBitCalculations,并提供了一种解决方案,采用数位DP方法来计算从0到给定整数n的所有数中出现的连续1的总数。

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

1032 - Fast Bit Calculations
Time Limit: 2 second(s)Memory Limit: 32 MB

A bit is a binary digit, taking a logical value of either 1 or 0 (also referred to as "true" or "false" respectively). And every decimal number has a binary representation which is actually a series of bits. If a bit of a number is 1 and its next bit is also 1 then we can say that the number has a 1 adjacent bit. And you have to find out how many times this scenario occurs for all numbers up to N.

Examples:

      Number         Binary          Adjacent Bits

         12                    1100                        1

         15                    1111                        3

         27                    11011                      2

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains an integer N (0 ≤ N < 231).

Output

For each test case, print the case number and the summation of all adjacent bits from 0 to N.

Sample Input

Output for Sample Input

7

0

6

15

20

21

22

2147483647

Case 1: 0

Case 2: 2

Case 3: 12

Case 4: 13

Case 5: 13

Case 6: 14

Case 7: 16106127360



题意:输入n,要求从0到n的所有数的二进制数里面总共有多少“11”。

思路:将输入的数转化为二进制之后进行数位dp。dp[i][j][k]表示i位数前一位是j(由于二进制只可能是0或1)已经存在k个11的总数。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
ll dp[40][2][40];
int digit[40];
ll dfs(int pos,int pre,int st,bool limit)
{
    if(pos==0)return st;
    if(!limit&&dp[pos][pre][st]!=-1)return dp[pos][pre][st];
    ll ans=0;
    int end=limit?digit[pos]:1;
    for(int i=0; i<=end; i++)
        ans+=dfs(pos-1,i,st+(pre&&i),limit&&(i==end));
    if(!limit)
        dp[pos][pre][st]=ans;
    return ans;
}
ll get(int x)
{
    int bj=0;
    while(x)
        digit[++bj]=x%2,x/=2;
    return dfs(bj,0,0,1);
}
int main()
{
    memset(dp,-1,sizeof(dp));
    int t,o=1;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        printf("Case %d: %lld\n",o++,get(n));///light oj里面只能用lld
    }
    return 0;
}


转载于:https://www.cnblogs.com/martinue/p/5547201.html

### XTU OJ 平台上的奇偶数位问题解析 处理奇偶数位的问题通常涉及对整数的每一位进行分析,判断其是处于奇数位置还是偶数位置。这类问题可以通过多种方法来实现,包括但不限于逐位取模运算和除法操作。 对于给定的一个正整数 `num`,可以采用如下算法来区分并计算奇偶位上的数值: #### 方法一:字符串转换法 通过将数字转化为字符串形式,可以直接访问每一个字符的位置索引来判定该位是在奇数位置还是偶数位置上[^1]。 ```python def odd_even_digits(num): num_str = str(num) result = {"odd": [], "even": []} for i, digit in enumerate(num_str): if (i % 2 == 0): # 如果索引为偶数,则认为这是奇数位(从左到右计数) result["odd"].append(int(digit)) else: result["even"].append(int(digit)) return result ``` 此函数返回一个字典,分别存储位于奇数位和偶数位上的所有数字列表。 #### 方法二:数学运算方式 不依赖于字符串转化,而是利用整数除法和取余操作逐步提取每位数字,并根据当前迭代次数决定是否属于奇数位或偶数位[^2]。 ```python def process_odd_even_positions(number): position = 1 # 初始化位置标记变量 even_sum = 0 odd_sum = 0 while number != 0: current_digit = number % 10 if position % 2 == 0: even_sum += current_digit else: odd_sum += current_digit number //= 10 position += 1 return f"Odd positions sum={odd_sum}, Even positions sum={even_sum}" ``` 上述代码实现了累加奇数位与偶数位各自对应的值总和的功能。 为了更好地理解如何应用这些技巧解决问题,在实际比赛中可能会遇到更复杂的要求,比如统计特定范围内满足某些条件的数字数量等扩展需求。因此建议多做练习加深理解和掌握不同场景下的解决方案设计能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值