题目描述

让我们来考虑1到N的正整数集合。让我们把集合中的元素按照字典序排列,例如当N=11时,其顺序应该为:1,10,11,2,3,4,5,6,7,8,9。

定义K在N个数中的位置为Q(N,K),例如Q(11,2)=4。现在给出整数K和M,要求找到最小的N,使得Q(N,K)=M。

输入输出格式

输入格式:

输入文件只有一行,是两个整数K和M。

输出格式:

输出文件只有一行,是最小的N,如果不存在这样的N就输出0。

输入输出样例

输入样例#1:
复制

  1. 2 4
输出样例#1: 复制

  1. 11
输入样例#2: 复制

  1. 100000001 1000000000
输出样例#2: 复制

  1. 100000000888888879

说明

【数据约定】

40%的数据,1<=K,M<=10^5;

100%的数据,1<=K,M<=10^9。

很像数位dp是吧;

不得不说还是我太菜了;

其实直接暴力算即可;

以233为例,

我们可以先计算出它最小应该在什么位置,然后与M进行比较,

如果M还有剩余,那么扩展位数慢慢加上,由于是*10的递增,复杂度是log的;

可以跑的很快;

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstdlib>
  5. #include<cstring>
  6. #include<string>
  7. #include<cmath>
  8. #include<map>
  9. #include<set>
  10. #include<vector>
  11. #include<queue>
  12. #include<bitset>
  13. #include<ctime>
  14. #include<deque>
  15. #include<stack>
  16. #include<functional>
  17. #include<sstream>
  18. //#include<cctype>
  19. //#pragma GCC optimize(2)
  20. using namespace std;
  21. #define maxn 1000005
  22. #define inf 0x7fffffff
  23. //#define INF 1e18
  24. #define rdint(x) scanf("%d",&x)
  25. #define rdllt(x) scanf("%lld",&x)
  26. #define rdult(x) scanf("%lu",&x)
  27. #define rdlf(x) scanf("%lf",&x)
  28. #define rdstr(x) scanf("%s",x)
  29. typedef long long ll;
  30. typedef unsigned long long ull;
  31. typedef unsigned int U;
  32. #define ms(x) memset((x),0,sizeof(x))
  33. const long long int mod = 1e9 + 7;
  34. #define Mod 1000000000
  35. #define sq(x) (x)*(x)
  36. #define eps 1e-4
  37. typedef pair<int, int> pii;
  38. #define pi acos(-1.0)
  39. //const int N = 1005;
  40. #define REP(i,n) for(int i=0;i<(n);i++)
  41. typedef pair<int, int> pii;
  42. inline ll rd() {
  43. ll x = 0;
  44. char c = getchar();
  45. bool f = false;
  46. while (!isdigit(c)) {
  47. if (c == '-') f = true;
  48. c = getchar();
  49. }
  50. while (isdigit(c)) {
  51. x = (x << 1) + (x << 3) + (c ^ 48);
  52. c = getchar();
  53. }
  54. return f ? -x : x;
  55. }
  56.  
  57. ll gcd(ll a, ll b) {
  58. return b == 0 ? a : gcd(b, a%b);
  59. }
  60. int sqr(int x) { return x * x; }
  61.  
  62. /*ll ans;
  63. ll exgcd(ll a, ll b, ll &x, ll &y) {
  64. if (!b) {
  65. x = 1; y = 0; return a;
  66. }
  67. ans = exgcd(b, a%b, x, y);
  68. ll t = x; x = y; y = t - a / b * y;
  69. return ans;
  70. }
  71. */
  72.  
  73. int T;
  74. int K, M;
  75. ll maxx[21];
  76. int dig[2100];
  77. int a[2000];
  78. ll qpow(int x) {
  79. ll ans = 1;
  80. for (int i = 1; i <= x; i++)ans *= 10;
  81. return ans;
  82. }
  83.  
  84. int main() {
  85. //ios::sync_with_stdio(0);
  86. cin >> T;
  87. maxx[0] = 1;
  88. for (int i = 1; i <= 20; i++)maxx[i] = maxx[i - 1] * 10;
  89. while (T--) {
  90. rdint(K); rdint(M);
  91. bool fg = 0;
  92. for (int i = 0; i < 19; i++) {
  93. if (K == maxx[i] && M != i + 1) {
  94. cout << 0 << endl;
  95. fg = 1; break;
  96. }
  97. }
  98. if (fg)continue;
  99. ll tp = K; int tot = 0; ms(dig);
  100. while (tp) {
  101. int y = tp % 10;
  102. dig[++tot] = y; tp /= 10;
  103. }
  104. for (int i = 1; i <= tot; i++) {
  105. a[i] = dig[tot - i + 1];
  106. }
  107. tp = 1; ll reg = 0; ll tmp = 0;
  108. for (int i = 1; i <= tot; i++) {
  109. reg = reg * 10ll + a[i];
  110. tmp += (reg - qpow(i - 1) + 1);
  111. }
  112. if (tmp > M) {
  113. cout << 0 << endl; continue;
  114. }
  115. if (tmp == M) { cout << K << endl; continue; }
  116. tp = 1; M -= tmp;
  117. ll number = 1ll * reg * 10 - qpow(tot + tp - 1);
  118. while (number < M) {
  119. M -= number; number *= 10; tp++;
  120. }
  121. cout << qpow(tp + tot - 1) + M - 1 << endl;
  122. }
  123. return 0;
  124. }

有趣的数 zoj 月赛的更多相关文章

  1. nyoj 85 有趣的数

    点击打开链接 有趣的数 时间限制:3000 ms  |  内存限制:65535 KB 难度: 描述 把分数按下面的办法排成一个数表. 1/1 1/2 1/3 1/4..... 2/1 2/2 2/3. ...

  2. CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

  3. P2022 有趣的数

    P2022 有趣的数 题目描述 让我们来考虑1到N的正整数集合.让我们把集合中的元素按照字典序排列,例如当N=11时,其顺序应该为:1,10,11,2,3,4,5,6,7,8,9. 定义K在N个数中的 ...

  4. CCF软考---《有趣的数》

    脑子一热报了CCF的软测..但是又觉得好像并没有什么卵用,就当为蓝桥杯预热然后顺便去软件学院玩一玩吧,遇到一个有意思的题: time limits : 1s 问题描述 我们把一个数称为有趣的,当且仅当 ...

  5. CCF系列之有趣的数(201312-4)

    题目链接: http://115.28.138.223:81/view.page?opid=4 试题名称: 有趣的数 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 我们把一个 ...

  6. CSP201312-4 有趣的数【dp】

    问题描述 试题编号: 201312-4 试题名称: 有趣的数 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, ...

  7. ccf 201312-04 有趣的数(组合数学)

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

  8. 洛谷 P2022 有趣的数 解题报告

    P2022 有趣的数 题目描述 让我们来考虑1到N的正整数集合.让我们把集合中的元素按照字典序排列,例如当N=11时,其顺序应该为:1,10,11,2,3,4,5,6,7,8,9. 定义K在N个数中的 ...

  9. CCF CSP 201312-4 有趣的数

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201312-4 有趣的数 问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0 ...

随机推荐

  1. jenkins基础知识

    修改默认端口号启动: java -jar jenkins.war --ajp13Port=-1 --httpPort=8089 一些基本的命令: http://[jenkins-server]/[co ...

  2. python对MySQL进行数据的插入、更新和删除之后需要commit,数据库才会真的有数据操作。(待日后更新)

    今天在尝试用下面的python代码对MySQL进行数据的插入.更新和删除时, 突然发现代码执行成功, 通过代码查询也显示数据已经插入或更新, 但是当我在MySQL客户端通过SQL语句查询时, 数据库中 ...

  3. 数据库最佳实践:DBA小马如何走上升值加薪之路?

    DBA可能是互联网公司里面熬夜最多,背锅最多的岗位之一,腾讯云数据库团队的同学结合自身的成长经历,用漫画的形式为我们分享了一位DBA是如何从菜鸟成长为大神,走上升职加薪,迎娶白富美之路的. 此文已由作 ...

  4. springboot启动过程(2)-run方法

    1 springApplication的run run方法主要是用于创造spring容器ConfigurableApplicationContext对象. public ConfigurableApp ...

  5. 除了ROS ,机器人自主定位导航还能怎么做?

    博客转载自:https://www.leiphone.com/news/201609/10QD7yp7JFV9H9Ni.html 雷锋网(公众号:雷锋网)按:本文作者科技剪刀手,思岚科技技术顾问. 随 ...

  6. 算法Sedgewick第四版-第1章基础-012一用stack实现输出一个数的二进制形式

    @Test public void e1_3_5() { Stack<Integer> stack = new Stack<Integer>(); int N = 7; whi ...

  7. Person的delete请求--------详细过程

    首先,数据库的增删改查都是在PersonRepository中实现,因此,直接进入PersonRepository,找到其父类,搜索delete. @Override @TransactionalMe ...

  8. 数据结构_Summary

    问题描述 可怜的 Bibi 丢了好几台手机以后,看谁都像是小偷,他已经在小本本上记下了他认为的各个地点的小偷数量.现在我们将 Bibi 的家附近的地形抽象成一棵有根树. 每个地点都是树上的一个节点,节 ...

  9. 安装thrift时,注意openssl参数

    在安装基于openssl-1.0.1c的thrift-0.9.0时,正常使用--with-openssl在configure时会出错,报"Error: libcrypto required. ...

  10. IIC协议解释

    IIC协议解释 (1)概述 I2C(Inter-Integrated Circuit BUS) 集成电路总线,该总线由NXP(原PHILIPS)公司设计,多用于主控制器和从器件间的主从通信,在小数据量 ...