题目描述

让我们来考虑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:
复制

2 4
输出样例#1: 复制

11
输入样例#2: 复制

100000001 1000000000
输出样例#2: 复制

100000000888888879

说明

【数据约定】

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

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

很像数位dp是吧;

不得不说还是我太菜了;

其实直接暴力算即可;

以233为例,

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

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

可以跑的很快;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 1000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int T;
int K, M;
ll maxx[21];
int dig[2100];
int a[2000];
ll qpow(int x) {
ll ans = 1;
for (int i = 1; i <= x; i++)ans *= 10;
return ans;
} int main() {
//ios::sync_with_stdio(0);
cin >> T;
maxx[0] = 1;
for (int i = 1; i <= 20; i++)maxx[i] = maxx[i - 1] * 10;
while (T--) {
rdint(K); rdint(M);
bool fg = 0;
for (int i = 0; i < 19; i++) {
if (K == maxx[i] && M != i + 1) {
cout << 0 << endl;
fg = 1; break;
}
}
if (fg)continue;
ll tp = K; int tot = 0; ms(dig);
while (tp) {
int y = tp % 10;
dig[++tot] = y; tp /= 10;
}
for (int i = 1; i <= tot; i++) {
a[i] = dig[tot - i + 1];
}
tp = 1; ll reg = 0; ll tmp = 0;
for (int i = 1; i <= tot; i++) {
reg = reg * 10ll + a[i];
tmp += (reg - qpow(i - 1) + 1);
}
if (tmp > M) {
cout << 0 << endl; continue;
}
if (tmp == M) { cout << K << endl; continue; }
tp = 1; M -= tmp;
ll number = 1ll * reg * 10 - qpow(tot + tp - 1);
while (number < M) {
M -= number; number *= 10; tp++;
}
cout << qpow(tp + tot - 1) + M - 1 << endl;
}
return 0;
}

最新文章

  1. sublime text常用快捷键
  2. 2014 项目中用到batik
  3. web基础----&gt;request的请求参数分析
  4. OC 解决NSArray、NSDictionary直接打印中文出现乱码的问题
  5. 集合运算(A-B)U(B-A)
  6. C++_Eigen函数库用法笔记——Matrix and Vector Arithmetic
  7. 如何使用SnpEff 对SNP结果进行分析
  8. 【BZOJ 2809】 [Apio2012]dispatching
  9. C#: 方法的默认参数
  10. 使用java.util.Timer来周期性的执行制定的任务
  11. 第4章 同步控制 Synchronization ----死锁(DeadLock)
  12. Treemap 有序的hashmap。用于排序
  13. centos关机与重启命令
  14. redis分页
  15. 函数防抖 debounce
  16. JAVA实现具有迭代器的线性表(顺序表)
  17. Centos7安装32位库用来安装32位软件程序
  18. request.getServletContext()
  19. Hp &amp;&amp; Dell服务器硬件监控
  20. css ! important 兼容性的一点测试

热门文章

  1. iphone配置实用工具iPhone Configuration Utility
  2. 第五章 JVM调优(待续)
  3. leetcode377
  4. 部署和调优 1.1 nfs部署和优化-2
  5. Yahoo浮沉录
  6. 分步编译一个C语言文件
  7. pow求一个数的n次幂
  8. 【总结整理】webGIS须知
  9. Codeforces 1097F Alex and a TV Show (莫比乌斯反演)
  10. 常用linux配置文件