参照了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. 【Validate Binary Search Tree】cpp

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  2. C Shell 中的特殊变量

    恢复 $0,当前脚本的文件名 $n,传递给脚本或函数的参数,n是一个数字,表示第几个参数 $#,传递给脚本或函数的参数个数 $*,传递给脚本或函数的所有参数 $?,函数的返回值 $$,当前shell的 ...

  3. Google Guava学习笔记——基础工具类Joiner的使用

    Guava 中有一些基础的工具类,如下所列: 1,Joiner 类:根据给定的分隔符把字符串连接到一起.MapJoiner 执行相同的操作,但是针对 Map 的 key 和 value. 2,Spli ...

  4. hdu 1043 Eight 经典八数码问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...

  5. 【BZOJ】【2084】【POI2010】Antisymmetry

    Manacher算法 啊……Manacher修改一下就好啦~蛮水的…… Manacher原本是找首尾相同的子串,即回文串,我们这里是要找对应位置不同的“反回文串”(反对称?233) 长度为奇数的肯定不 ...

  6. AVFoundation的使用

    AVFoundation的使用 2013-05-03 14:50:21|  分类: iphone_dev_note|举报|字号 订阅         相机相关应用一般会用到AVFoundation. ...

  7. 项目分析 NGPcontext

    NGPcontext 之前对这个一直很疑惑,我一直认为只是在机器人方面有用处,但很有疑问,正在做这方面,我想好好看看到底是怎么运行的 bool NGP::init(NGPcontext context ...

  8. POI中设置Excel单元格格式

    引用:http://apps.hi.baidu.com/share/detail/17249059 POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWork ...

  9. IIS Express 及 vs2008下使用IIS Express

    介绍 IIS Express 开发 ASP.NET 的应用程序是我的主要工作.当然我会选择最适合的开发环境.客户多属于企业用户,我的开发的选择,多半是 ASP.NET Web Application ...

  10. Hadoop分布式配置

    本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 请先参照Linux安 ...