URAL 1233 Amusing Numbers 好题
参照了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 好题的更多相关文章
- URAL 1233 - Amusing Numbers
首先计算出k至少为第几位,如果m小于这个数,那么输出0 还有一种情况, 就是10的i次方的这种情况,如果i+1等于m,那么直接输出k,否则输出0 其他的情况,就是二分,然后判断计算其插入到k之前的数的 ...
- 递推DP URAL 1586 Threeprime Numbers
题目传送门 /* 题意:n位数字,任意连续的三位数字组成的数字是素数,这样的n位数有多少个 最优子结构:考虑3位数的数字,可以枚举出来,第4位是和第3位,第2位组成的数字判断是否是素数 所以,dp[i ...
- 递推DP URAL 1009 K-based Numbers
题目传送门 题意:n位数,k进制,求个数分析:dp[i][j] 表示i位数,当前数字为j的个数:若j==0,不加dp[i-1][0]; 代码1: #include <cstdio> #in ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
- ural 1150. Page Numbers
1150. Page Numbers Time limit: 1.0 secondMemory limit: 64 MB John Smith has decided to number the pa ...
- Educational Codeforces Round 2 A. Extract Numbers 模拟题
A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- URAL 1012 K-based Numbers. Version 2(DP+高精度)
题目链接 题意 :与1009一样,不过这个题的数据范围变大. 思路:因为数据范围变大,所以要用大数模拟,用java也行,大数模拟也没什么不过变成二维再做就行了呗.当然也可以先把所有的都进行打表,不过要 ...
- 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 ...
- Educational Codeforces Round 13 A. Johny Likes Numbers 水题
A. Johny Likes Numbers 题目连接: http://www.codeforces.com/contest/678/problem/A Description Johny likes ...
随机推荐
- windows 2008 下C#调用office组件访问拒绝的解决方法(failed due to the following error: 80070005 拒绝访问)
"组件服务"- >"计算机"- >"我的电脑"- >"DCOM配置"->找到word->属 ...
- 为什么匿名内部类参数必须为final类型(转载)
为什么匿名内部类参数必须为final类型转自于:http://feiyeguohai.iteye.com/blog/1500108 1) 从程序设计语言的理论上:局部内部类(即:定义在方法中的内部类 ...
- Hibernate映射类型对照表
Hibernate映射类型对照表 java类型 Hibernate映射类型 SQL类型 java.math.BigDecimal big_decimal numeric byte[] binary ...
- java 验证日期
- Leetcode#147 Insertion Sort List
原题地址 心得:有关链表的题目,多用中间变量,代码写得清晰一点,适当注释 代码: ListNode *insertionSortList(ListNode *head) { if (!head) re ...
- 在线编辑器 (UBB, FCK)
这里主要说明一下:UBB UBB 使用类型HTML的语法. UBB相对FCK的HTML方式, 安全性高. 用户不可以直接嵌入HTML代码. UBB 在线编辑器(JS版): http://www. ...
- XSS的原理分析与解剖(二)
0×01 前言: 上节(http://www.freebuf.com/articles/web/40520.html)已经说明了xss的原理及不同环境的构造方法.本期来说说XSS的分类及挖掘方法. ...
- WPF 详解模板
在WPF中有三大模板 ControlTemplate,ItemsPanelTemplate,DataTemplate.其中ControlTemplate和 ItemsPanelTemplate是控件模 ...
- jQuery一些常用特效方法使用实例
1. jQuery fadeIn() 用于淡入已隐藏的元素. 语法: $(selector).fadeIn(speed,callback); 实例: $("button").cli ...
- Mac下使用Apache TCPMon
Mac下使用Apache TCPMon 参考链接: TCPMon Tutorial Anyone know how to get TCPMON working on a mac? Apache TCP ...