参照了nocow上的解法,照搬过来……

易知一个数X在数列中在另一个数Y前,当且仅当X前缀小于Y或前缀相等X短,那么我们分布考虑,比如对于数48561:

5位上:10000~48560;

4位上:1000~4856;

3位上:100~485;

2位上:10~48;

1位上:1~4.

这样我们就得到了1..N中4856的位置,可以用这个来判错.

接下来的方法类似,为了使得我们的N满足要求,我们必须去找比48561大且前缀比其小的数,同样有:

6位上:100000~48569;  //注意红色部分比原数减少了1,之前没注意这里一直WA……

……

---------------------以上nocow上给出的题解-----------------------

判断无解的情况:

1.当K != 10^x 时,假设到 K 时前面已经有的数的个数是cnt,如果 M - 1 < cnt 则无解。

2.当K == 10^x 时,K前面最多只有x个数,M - 1 > x 则无解。

一组数据:

1150 27350

12 2

20 13

3 23

3 3

100 3

输出:

1010679

0

20

29

3

100

#include <cstdio>
#include <cstring>
#include <cstdlib> #define LL unsigned long long int using namespace std; const int MAXN = ; LL K, M, cnt;
LL bit[MAXN];
char strK[MAXN], strM[MAXN];
bool ok, find; void init()
{
bit[] = ;
for ( int i = ; i < MAXN; ++i )
bit[i] = bit[i - ] * (LL);
return;
} void chuli()
{
cnt = ;
int len = strlen( strK );
for ( int i = ; i <= len; ++i )
{
//printf( "###%I64u %I64u\n", K / bit[ len - i ], bit[i - 1] );
cnt += K / bit[ len - i ] - bit[i - ] + ;
}
--cnt;
//printf("**%I64u %I64u\n", cnt, M );
if ( cnt > M - ) ok = false;
return;
} LL GetAns()
{
LL cur = K; if ( cnt == M - ) return cur;
--cur; int i;
for ( i = strlen(strK); ; ++i )
{
cur = cur * + ;
//printf( "~~~%20I64u %20I64u\n", cur, bit[i] );
LL tmp = cnt + cur - bit[i] + ;
//printf( "tmp=%I64d\n", tmp );
if ( tmp == M - ) return cur;
if ( tmp >= M ) break;
cnt = tmp;
} return bit[i] + M - - cnt - ;
} bool SpecialJudge()
{
int len = strlen(strK);
find = false;
for ( int i = ; i <= len; ++i )
{
if ( K == bit[i] )
{
find = true;
break;
}
}
if ( !find ) return true;
if ( M - > cnt ) return false;
return true;
} int main()
{
//freopen( "out.txt", "r", stdin );
//freopen( "s1.txt", "w", stdout );
init();
while ( scanf( "%s", strK ) == )
{
scanf( "%I64u", &M );
sscanf( strK, "%I64u", &K ); ok = true;
LL ans;
chuli(); if ( !SpecialJudge() )
{
puts("");
continue;
}
if ( find )
{
if ( cnt == M - ) printf( "%I64u\n", K );
else puts("");
continue;
} if ( ok ) ans = GetAns();
if ( ok ) printf( "%I64u\n", ans );
else puts("");
}
return ;
}

URAL 1233 Amusing Numbers 好题的更多相关文章

  1. URAL 1233 - Amusing Numbers

    首先计算出k至少为第几位,如果m小于这个数,那么输出0 还有一种情况, 就是10的i次方的这种情况,如果i+1等于m,那么直接输出k,否则输出0 其他的情况,就是二分,然后判断计算其插入到k之前的数的 ...

  2. 递推DP URAL 1586 Threeprime Numbers

    题目传送门 /* 题意:n位数字,任意连续的三位数字组成的数字是素数,这样的n位数有多少个 最优子结构:考虑3位数的数字,可以枚举出来,第4位是和第3位,第2位组成的数字判断是否是素数 所以,dp[i ...

  3. 递推DP URAL 1009 K-based Numbers

    题目传送门 题意:n位数,k进制,求个数分析:dp[i][j] 表示i位数,当前数字为j的个数:若j==0,不加dp[i-1][0]; 代码1: #include <cstdio> #in ...

  4. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  5. ural 1150. Page Numbers

    1150. Page Numbers Time limit: 1.0 secondMemory limit: 64 MB John Smith has decided to number the pa ...

  6. Educational Codeforces Round 2 A. Extract Numbers 模拟题

    A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  7. URAL 1012 K-based Numbers. Version 2(DP+高精度)

    题目链接 题意 :与1009一样,不过这个题的数据范围变大. 思路:因为数据范围变大,所以要用大数模拟,用java也行,大数模拟也没什么不过变成二维再做就行了呗.当然也可以先把所有的都进行打表,不过要 ...

  8. ural 1013. K-based Numbers. Version 3(动态规划)

    1013. K-based Numbers. Version 3 Let’s consider K-based numbers, containing exactly N digits. We def ...

  9. Educational Codeforces Round 13 A. Johny Likes Numbers 水题

    A. Johny Likes Numbers 题目连接: http://www.codeforces.com/contest/678/problem/A Description Johny likes ...

随机推荐

  1. The finnacial statements,taxes and cash flow

    This chapter-2 we learn about the the financial statements(财务报表),taxes and cash flow.We must pay par ...

  2. Learning Java language Fundamentals

    Chapter 2 Learning Java language fundamentals exercises: 1.What  is Unicode? Unicode is a computing ...

  3. 学习Linux第五天

    1.VIM编辑器 3种模式: Command Model , Insert Model , Last line Model 安装vim: sudo apt-get install vim 如果提示出错 ...

  4. Mysql的主从数据库没有同步的解决办法

    Mysql的主从数据库没有同步的解决办法 今天发现Mysql的主从数据库没有同步 先上Master库: mysql>show processlist; 查看下进程是否Sleep太多.发现很正常. ...

  5. Hibernate中启用日志

    Problem How do you determine what SQL query is being executed by Hibernate? How can you see the Hibe ...

  6. Memcached使用

    一.Memcached简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网 ...

  7. poj 3237 Tree 树链剖分

    题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...

  8. 事务并发处理: DB+ORM+逻辑代码

    在学习了马士兵有关事务并发处理的视频后, 感觉对事务并发处理的概念,问题以及解决方式有了一定的了解,赶紧记录下来以备后用. 1. 事务:一系列操作要么都完成,要么一个都不完成 2. 事务并发:多个事务 ...

  9. CoreText 使用教程

    [iOS开发] CoreText 使用教程:以创建一个简单的杂志应用为例抢沙发 分类:iPhone开发 标签:CoreText.iOS.iOS开发.iOS开发教程.杂志应用 BBS.CHINAAPP. ...

  10. [geeksforgeeks] Count the number of occurrences in a sorted array

    Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a ...