HDU3652 B-number

本文介绍了一个特定问题的解决方法:如何找出1到n之间所有包含子串“13”且能被13整除的非负整数的数量。通过使用数位DP方法,文章详细解释了如何构建递归函数来解决这个问题。

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

B-number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7024    Accepted Submission(s): 4100


Problem Description
A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.
 

Input
Process till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).
 

Output
Print each answer in a single line.
 

Sample Input
  
  
13 100 200 1000
 

Sample Output
  
  
1 1 2 2
 

Author
wqb0039
 

Source

2010 Asia Regional Chengdu Site —— Online Contest

———————————————————————————————————

题目的意思是求1到n有多少个包含子串13并且能被13整除的数有几个

思路:数位dp,4维数组表示到len为止,以pre结尾的,和mod13后余sum的,是否已经有13的是有几个,dfs求解


#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 10000005
#define Mod 10001
using namespace std;
#define LL long long

LL dp[20][20][20][2];
int a[20],index[3000];
int mod;


int dfs(int len,int pre,int sta,int sum,bool limit)
{
    if(len<0)
        return sum==0&&sta;
    if(dp[len][pre][sum][sta]!=-1&&!limit)
        return dp[len][pre][sum][sta];
    int up=limit?a[len]:9;
    int ans=0;
    for(int i=0; i<=up; i++)
    {
        ans+=dfs(len-1,i,sta||(pre==1&&i==3),(sum*10+i)%13,limit&&i==up);
    }
    return limit?ans:dp[len][pre][sum][sta]=ans;
}

int solve(int x)
{
    int cnt=0;
    while(x>0)
    {
        a[cnt++]=x%10;
        x/=10;
    }
    return dfs(cnt-1,-1,0,0,1);
}

int main()
{
    int n;
    memset(dp,-1,sizeof dp);
    while(~scanf("%d",&n))
    {
        printf("%d\n",solve(n));
    }
    return 0;
}


### HDU 1443 约瑟夫问题解析 #### 题目描述 题目涉及的是经典的约瑟夫环问题的一个变种。给定一个整数 \( k \),表示有 \( k \) 个好人和 \( k \) 个坏人,总共 \( 2k \) 个人围成一圈。编号从 1 到 \( 2k \),其中前 \( k \) 个为好人,后 \( k \) 个为坏人。目标是在不杀死任何好人的前提下,找到可以先消灭所有坏人的最小步数 \( n \)[^5]。 #### 解题思路 为了确保在杀掉第一个好人之前能将所有的坏人都清除,可以通过模拟约瑟夫环的过程来寻找符合条件的最小步数 \( n \)。一种有效的方法是利用动态规划的思想逐步缩小范围直到找到最优解。对于较大的 \( k \),由于数值较大可能导致计算复杂度增加,因此需要考虑算法效率并进行适当优化[^1]。 #### Python 实现代码 下面提供了一个基于Python编写的解决方案: ```python def josephus(k): m = 2 * k def find_min_n(m, start=1): for n in range(1, m + 1): pos = (start + n - 2) % m + 1 if all((pos - i) % m > k or (pos - i) % m == 0 for i in range(n)): return n raise ValueError("No solution found") min_n = None for good_start in range(1, k + 1): try: current_min = find_min_n(m=m, start=good_start) if not min_n or current_min < min_n: min_n = current_min except ValueError as e: continue return min_n if __name__ == "__main__": test_cases = [int(input()) for _ in range(int(input()))] results = [] for case in test_cases: result = josephus(case) print(result) ``` 此段代码实现了上述提到的逻辑,并且能够处理多个测试案例。需要注意的是,在实际应用中可能还需要进一步调整参数以及边界条件以适应不同情况下的需求[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值