跳到正文

个人记录

yezian

Pikachu_q_w_q约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年4月12日
收录
2026年10月3日
更新
2026年11月11日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int digit[20];
int dp[20][2][2];
int x;
int dfs(int pos, bool limit, bool lead)
{
    if(pos==0)
        return 1;
    if(dp[pos][limit][lead]!=-1)
        return dp[pos][limit][lead];
    int ans=0;
    int up=limit? digit[pos]:9;
    for(int i=0;i<=up;i++)
    {
        if(i==x) continue;
        bool new_limit=limit&&(i==up);
        bool new_lead=lead&&(i==0);
        if(lead&&i==0)
            ans+=dfs(pos-1,new_limit,new_lead);
        else
            ans+=dfs(pos-1,new_limit,new_lead);
    }
    return dp[pos][limit][lead]=ans;
}

int count(int n)
{
    if(n<0) return 0;
    int cnt=0;
    int tmp=n;
    while(tmp)
    {
        digit[++cnt]=tmp%10;
        tmp/=10;
    }
    memset(dp,-1,sizeof(dp));
    return dfs(cnt,1,1);
}

signed main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n>>x;
        cout<<count(n)<<endl;
    }
    return 0;
}
原文