2019 杭电多校 8 1003

题目链接:HDU 6659

比赛链接:2019 Multi-University Training Contest 8

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 \(10^{1000}\), 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\le q\le 1500)\), denoting the number of test cases. Each test case is a single line of two integers \(d (1\le d\le 9)\) and \(x (0\le x\le 10^{18})\).

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

Solution

题意

定义 \(f(d, n)\) 为十进制下 \(1\) 到 \(n\) 所有数的数位中数字 \(d\) 出现的次数。给定 \(x\),找出最大的 \(n(n \le x)\) 满足 \(f(d, n) = n\)。

题解

看到了一个神仙做法。

显然如果 \(f(d, x) = x\) 时就直接输出。

否则,需要缩小 \(x\)。令 \(f(d, x) = y\),则需要将 \(x\) 缩小 \(\lceil \frac{|x - y|}{18} \rceil\)。即 \(x = x - abs(f(d, x) - x) / 18\)。原因是 \(f(d, x)\) 与 \(f(d, x - 1)\) 最多相差 \(18\) 个 \(d\) \(\ (e.g. \ f(9, 10^{18}-1)\ to\ f(9, 10^{18}-2))\)。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; // 计算 1 到 n 中数字 x 出现的次数
ll f(ll d, ll n) {
ll cnt = 0, k;
for (ll i = 1; k = n / i; i *= 10) {
cnt += (k / 10) * i;
int cur = k % 10;
if (cur > d) {
cnt += i;
}
else if (cur == d) {
cnt += n - k * i + 1;
}
}
return cnt;
} int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
ll d, x;
cin >> d >> x;
while (true) {
ll num = f(d, x);
if (num == x) {
cout << x << endl;
break;
} else {
x -= max(1LL, abs(num - x) / 18);
}
}
}
return 0;
}

Reference

2019 Multi-University Training Contest 8——Acesrc and Good Numbers(数学 想法)

HDU 6659 Acesrc and Good Numbers (数学 思维)的更多相关文章

  1. POJ2402/UVA 12050 Palindrome Numbers 数学思维

    A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...

  2. HDU 2674 N!Again(数学思维水题)

    题目 //行开始看被吓一跳,那么大,没有头绪, //看了解题报告,发现这是一道大大大的水题,,,,,//2009 = 7 * 7 * 41//对2009分解,看它有哪些质因子,它最大的质因子是41,那 ...

  3. POJ 3252 Round Numbers 数学题解

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  4. 程序设计中的数学思维函数总结(代码以C#为例)

    最近以C#为例,学习了程序设计基础,其中涉及到一些数学思维,我们可以巧妙的将这些逻辑问题转换为代码,交给计算机运算. 现将经常会使用到的基础函数做一总结,供大家分享.自己备用. 1.判断一个数是否为奇 ...

  5. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  6. UVa10025 The ? 1 ? 2 ? ... ? n = k problem 数学思维+规律

    UVa10025 ? 1 ? 2 ? ... ? n = k problem The problem Given the following formula, one can set operator ...

  7. B. Tell Your World(几何数学 + 思维)

    B. Tell Your World time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. HDU - 6409:没有兄弟的舞会(数学+思维)

    链接:HDU - 6409:没有兄弟的舞会 题意: 题解: 求出最大的 l[i] 的最大值 L 和 r[i] 的最大值 R,那么 h 一定在 [L, R] 中.枚举每一个最大值,那么每一个区间的对于答 ...

  9. HDU 4611 Balls Rearrangement (数学-思维逻辑题)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611 题意:给你一个N.A.B,要你求 AC代码: #include <iostream> ...

随机推荐

  1. JS-插件编写

    # 参数处理 JS: function plugin_mian_func(options){ var defaluts = { opt1: 'opt1', opt2: 'opt2', opt3: { ...

  2. FPGA 报错ERROR:Simulator:861 – Failed to link the design。

    问题综述: 我使用的是windows 10 32位专业版系统,电脑装的是ISE14.4版本,当我用此ISE自带的仿真器ISIM来仿真时,仿真器总是报错ERROR:Simulator:861 – Fai ...

  3. 3.Jmeter 快速入门教程(三-1) --添加响应断言(即loadrunner中所指的检查点)

    上一节课,我们创建了一个测试场景,并进行了少量vuser的负载测试. 有时候我们执行了测试,但是发现并不是所有事务都执行成功了. 那是因为我们只是发起了测试,但并没有对每次请求测试的返回作校验. 所以 ...

  4. 第四周总结和实验二Java简单类与对象

    实验目的 掌握类的定义,熟悉属性.构造函数.方法的使用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实列的方法和属性: 理解static修饰对类. ...

  5. 在JMeter测试计划中如何控制业务比例

    作者:Selingchen 来源:CSDN 原文:https://blog.csdn.net/selingchen/article/details/47844375

  6. shell编程:变量替换

    定义变量:$ var_1="I love you, Do you love me" 输出变量:$ echo $var_1 打印结果:I love you, Do you love ...

  7. spring 注入bean的两种方式

    我们都知道,使用spring框架时,不用再使用new来实例化对象了,直接可以通过spring容器来注入即可. 而注入bean有两种方式: 一种是通过XML来配置的,分别有属性注入.构造函数注入和工厂方 ...

  8. Ubuntu 14.04 Sublime Text3 Java编译运行(最简单的方法)

    Sublime,结果发现只能编译,无法直接运行,于是就在网上搜解决方法,发现大部分方法都是告诉你要进入Java.sublime-packag这个文件,然后再修改JavaC.sublime-build, ...

  9. CentOs如何找回root密码

    当CentOs的root密码忘了的时候,可以进入单用户模式,更改一下root密码就可以了. 具体操作如下 1.重启Linux后,在此界面,3秒内按下回车键 2.出现此界面 3.按e键,进入下图,然后把 ...

  10. mysql的事务四个特性以及 事务的四个隔离级别

    一.事务四大属性 分别是原子性.一致性.隔离性.持久性. 1,原子性(Atomicity) 原子性是指事务包含的所有操作要么全部成功,要么全部失败回滚,因此事务的操作如果成功就必须要完全应用到数据库, ...