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

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  2. 设计模式 --深入理解javascript

    /* 一.单例模式 */ var Universe; (function () { var instance; Universe = function Universe() { if (instanc ...

  3. ngcordova 监控网络制式改变

    ngcordova 监控网络制式改变 keywords cordova,phonegap,ionic,network,网络制式 API参考 http://ngcordova.com/docs/plug ...

  4. C++ Templates基本知识

    一.使用Templates的原因:例如我们要写一个算法,由于类型不同,我们不得不做一下工作.1.使用宏定义代替Templates不利于类型检查. 2.将一些算法放在基类里,以后的扩展的子类都需要充基类 ...

  5. SPOJ-SQRBR Square Brackets

    原题传送:http://www.spoj.pl/problems/SQRBR 动态规划. 设f[i][j]表示前i个位置在合法情况下缺少j个右括号的方案数. 转移方程为: f[i][j] = f[i- ...

  6. zoj 2314 Reactor Cooling 网络流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 The terrorist group leaded by a ...

  7. matrix_last_acm_4

    2013 ACM-ICPC吉林通化全国邀请赛 A http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97654#problem/A 题意:输入 ...

  8. 2015 WEB前端学习路线图

    2015 WEB前端学习路线图,欢迎小伙伴补充 @落雨

  9. uiwebview和 js交互框架

    WebViewJavascriptBridge

  10. quickeys 过期解决办法

    自己在系统中找了大半天,终于发现了一个叫.com.quickeys.quickeysX3.plist的文件,存在于硬盘的 ~/Library/Preferences/ 目录下,直觉感觉文件的大体内容应 ...