HDOJ 6659 Acesrc and Good Numbers

本文探讨了在给定范围内寻找最大d-good数的问题,即一个数等于其十进制表示中某数字出现的次数。通过分析和使用数位DP或数学公式计算d在1到n内的出现次数,提出了一种迭代逼近的解决方案。该方法通过调整候选数来逐步逼近目标,最终找到符合条件的最大数。

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

题目地址
Problem Description
Acesrc is a famous mathematician at Nanjing University second to none. Playing with interesting numbers is his favorite. Today, he finds a manuscript when cleaning his room, which reads

… Let f(d,n) denote the number of occurrences of digit d in decimal representations of integers 1,2,3,⋯,n. The function has some fantastic properties …

… Obviously, there exist some nonnegative integers k, such that f(d,k)=k, and I decide to call them d-good numbers …

… I have found all d-good numbers not exceeding 101000, but the paper is too small to write all these numbers …

Acesrc quickly recollects all d-good numbers he found, and he tells Redsun a question about d-good numbers: what is the maximum d-good number no greater than x? However, Redsun is not good at mathematics, so he wants you to help him solve this problem.

Input
The first line of input consists of a single integer q (1≤q≤1500), denoting the number of test cases. Each test case is a single line of two integers d (1≤d≤9) and x (0≤x≤1018).

Output
For each test case, print the answer as a single integer in one line. Note that 0 is trivially a d-good number for arbitrary d.

Sample Input
3
1 1
1 199999
3 0

Sample Output
1
199990
0

  • 赛后发现把某个int改成longlong就过了,艹,不开ll见祖宗
  • 思路
    首先我们要解决的是计算n以内d的出现次数,方法可以用数位dp或者找规律推数学公式,恰好我以前看过一篇博客,讲了数学公式的推导,传送门 ,里面讲的很详细了,我也不多说废话。接下来就是找答案了,显然如果f(d,n)=n答案就为n。否则,让n-=max(1,abs(f(d,n)-n)/18)。因为*f(d,n)f(d,n-1)*的差最多为18。显然,n和abs(f(d,n)-n)/18的数量级是一样的,故循环的次数不大。
  • 代码
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
ll cal(ll n, ll x) {
    ll cnt = 0, k;
    for (ll i = 1;k = n / i;i *= 10) {
        cnt += (k / 10) * i;
        int cur = k % 10;
        if (cur > x) {
            cnt += i;
        } else if (cur == x) {
            cnt += n - k * i + 1;
        }
    }
    return cnt;
}
int main(){
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    ll d,x;
    while(T--){
        cin>>d>>x;
        while(1){
            ll num=cal(x,d);
            if(num==x){
                cout<<x<<endl;
                break;
            }
            x-=max(1ll,abs(num-x)/18);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值