题目传送门Little Sub and Mr.Potato's Math Problem


Time Limit: 2 Seconds      Memory Limit: 65536 KB

Little Sub loves math very much. He enjoys counting numbers.

One day, Mr.Potato gives him an interesting math problem. Please help Little Sub solve this problem.

Let's sort the integers  according to alphabetical order. For example, when , the order should be: .

We define  as the position of number  in the sorted  numbers. For example, .

Given  and , please find the smallest  such that .

Input

There are multiple test cases. The first line of the input contains an integer  (), indicating the number of test cases. For each test case:

The first and only line contains two integers  and  (, ).

Output

For each test case, please output the answer in one line. If there is no such , please output "0" (without quotes).

Sample Input

2
2 4
10000001 100000000

Sample Output

11
1000000088888880 题意:如上题 思路:

第一步:我们可以先计算出当前的数前面按照字典序的话,前面有多少数(包括自己)。

(具体计算过程,456.首先是三位数的数在456前面的个数,就是(100->456),然后再就是两位数(10-45),然后再就是一位数1-4,严格遵守字典序)。

第二步:如果说当前的数前面的数的个数大于m的话,这个时候肯定是不符合情况的,如果说刚好凑起来的话,这个时候输出k就可以了。

第三步:经过了第二步,发现当前的数N取k的时候凑不起来,我们就需要通过增加n来使得k的位数往后移动。举个例子,456不够,我们就可以通过添加1000-4560之间的数来使得456的位数往后移动,如果还不够我们就添加10000-45600之间的数使得456的位数往后移动。

第四步:我们需要判断一种特殊情况,比如说 10 10这个样例,10前面的数只有1,但是你如果加100之后的,也不会使得10的位数往后移动,这个时候就应该输出0。(就是这些数的字典序是相对固定的)

代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int T;
int k,m;
ll base=; ll get_num(ll x)
{
ll ans=;
ll tmp=x;
while(x){
base*=;
x/=;
}
base/=;
ll base1=base;
while(tmp)
{
ans+=(tmp-base1)+;
tmp/=;
base1/=;
}
return ans;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&k,&m);
base=;
ll tmp=get_num(k);
m-=tmp;
if(m<) printf("0\n");
else if(m==) printf("%d\n",k);
else if(base==k) printf("0\n");
else{
ll ans=k-base;
base*=;
ans*=;
while(m>ans)
{
m-=ans;
ans*=;
base*=;
}
printf("%lld\n",base+m-);
}
}
return ;
}

参考博客:https://www.cnblogs.com/letlifestop/p/10294237.html

												

Little Sub and Mr.Potato's Math Problem (构造法)的更多相关文章

  1. (P2022 有趣的数)||(zoj Little Sub and Mr.Potato's Math Problem)(思维)

    题目链接:https://www.luogu.org/problemnew/show/P2022 题目大意:中文题目 具体思路: 第一步:我们可以先计算出当前的数前面按照字典序的话,前面有多少数(包括 ...

  2. Little Sub and Mr.Potato's Math Problem-构造

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5864 思路 : 判断小于它的合法的,再看大于它的合法的,特判10000. ...

  3. hdu 1757 A Simple Math Problem (构造矩阵解决递推式问题)

    题意:有一个递推式f(x) 当 x < 10    f(x) = x.当 x >= 10  f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + ...

  4. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. HDU1757 A Simple Math Problem 矩阵快速幂

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. HDU 5615 Jam's math problem

    Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is t ...

  7. hdu----(5055)Bob and math problem(贪心)

    Bob and math problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  9. BestCoder Round #70 Jam's math problem(hdu 5615)

    Problem Description Jam has a math problem. He just learned factorization. He is trying to factorize ...

随机推荐

  1. Python + logging输出到屏幕,将log日志写入到文件

    logging提供了一组便利的函数,用来做简单的日志.它们是 debug(). info(). warning(). error() 和 critical(). logging函数根据它们用来跟踪的事 ...

  2. 如何启用Nginx的status功能,查看服务器状态信息?

    如何查看服务器状态信息? 我们可以通过安装Nginx的功能模块,并修改Nginx的主配置文件来实现. 1.编译安装时使用--with-http_stub_status_module开启状态页面模块 [ ...

  3. 吉首大学2019年程序设计竞赛(重现赛)I 滑稽树上滑稽果 (莫队+逆元打表)

    链接:https://ac.nowcoder.com/acm/contest/992/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K  ...

  4. fork和vfork的区别

    参见百度百科API说明: fork 头文件: #include<unistd.h> #include<sys/types.h> 函数原型: pid_t fork( void); ...

  5. objectMaaper 反序列化json字段多于或少于实体处理

    两种方式 1 忽略json上未知的字段 设置实体类注解 @JsonIgnoreProperties(ignoreUnknown = true) public class Foo { ... } 2 配 ...

  6. CSP-S2019 停课日记

    前言 不想上文化课,于是就停课了 (雾) \(10.13\) 停课前一天 今天名义上是放假,所以不算停课. 老师和同学们听说我要停课,都十分的不舍.我啥也没说就悄悄溜到一中来了. \(10.14\) ...

  7. 文本框的SelectionDirection属性

    代码实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. Thinkphp 请求和响应

    一. Request对象获取方法 1. request() 助手函数获取 2. think\Request 类获取 3.利用框架注入Request对象  Request方法时单利方法 在think框架 ...

  9. python的次方操作

    好简单,不需要import任何包 b=a**n就是求a的n次方,如果n=0.5就是开方 如果开方的是负数或者附复数,需要 import math b=math.sqrt(a) 这样

  10. Linux g++ 编译添加 pthread

    If you are going to compile a C program with pthread.h in LINUX using GCC or G++ you will have to us ...