题目描述

让我们来考虑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;
}

有趣的数 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. windows异常演示,指定异常类型,然后生成异常

    #include "stdafx.h"#include <Windows.h>#include <float.h> DWORD Filter (LPEXCE ...

  2. 类型:.net;问题:asp.net window验证;结果:细说ASP.NET Windows身份认证

    细说ASP.NET Windows身份认证 阅读目录 开始 认识ASP.NET Windows身份认证 访问 Active Directory 在ASP.NET中访问Active Directory ...

  3. 问题:oracle decode;结果:oracle中的decode的使用

    oracle中的decode的使用 Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件 ...

  4. 部署和调优 1.3 pureftp部署和优化-1

    FTP 是 File Transfe Protocol(文件传输协议)的英文简称,而中文简称为 “文传协议” 用于 Internet 上的控制件的双向传输. 可以访问    www.pureftpd. ...

  5. mybaits中date类型显示时分秒(orcle数据库)

    <insert id="insert" parameterType="daSysLoginLog"> insert into DA_SYS_LOGI ...

  6. Hbase优化记录

    <configuration><property><name>hbase.rootdir</name><value>hdfs://gagcl ...

  7. Cookies和Session的定义与区别

    Cookies和Session二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录下来.当下次你再光临同一个网 ...

  8. java格式化数字、货币、金钱

    网上摘来的,以后可能会用到 java开发中经常会有数字.货币金钱等格式化需求,货币保留几位小数,货币前端需要加上货币符号等.可以用java.text.NumberFormat和java.text.De ...

  9. Node内存限制与垃圾回收

    对象分配 所有的JS对象都是通过堆来进行分配的.使用process.memoryUsage()查看使用情况Node.js 中文网文档 process.memoryUsage() { rss: , he ...

  10. iOS  地图(自定义地位图标)

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...