test20181016 B君的第一题
题意


分析
考场爆零做法
考虑位数少的一定更小,高位小的一定更少。
然后计算一定位数下不同数字的个数,然后从高到低依次确定数位。
特例:如果确定的高位的后缀出现了x,那么要把x调整到后缀去,这样一定更优。
然而这样做有问题,有重复的情况,譬如样例1的6666。
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#pragma GCC optimize ("O0")
using namespace std;
template<class T> inline T read(T&x)
{
T data=0;
int w=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
return x=data*w;
}
typedef long long ll;
const int INF=0x7fffffff;
const int MAXN=2e3+7;
char X[MAXN],ans[MAXN];
int len;
ll n;
int main()
{
freopen("python.in","r",stdin);
freopen("python.out","w",stdout);
scanf("%s",X+1);
len=strlen(X+1);
read(n);
if(n==1)
{
puts(X+1);
return 0;
}
n-=1;
int lim;
for(lim=1;;++lim)
{
ll sum=(ll)lim*9*pow(10,lim-1)+pow(10,lim);
cerr<<"lim="<<lim<<" sum="<<sum<<endl;
if(n>sum)
n-=sum;
else
break;
}
cerr<<"n="<<n<<" lim="<<lim<<endl;
for(int i=1;i<=lim;++i)
{
cerr<<"i="<<i<<endl;
for(int j=(i==1?1:0);j<=9;++j)
{
ll sum=(ll)(lim-i+1)*pow(10,lim-i);
// cerr<<" j="<<j<<" sum="<<sum<<endl;
if(n>sum)
n-=sum;
else
{
ans[i]=j+'0';
ans[i+1]=0;
break;
}
if(j==9)
abort();
}
cerr<<"ansi="<<ans[i]<<endl;
if(i>=len&&strcmp(ans+i-len+1,X+1)==0)
{
// cerr<<"n="<<n<<endl;
int j;
for(j=lim+len;n;--j,n/=10)
{
ans[j]=n%10+'0';
}
for(;j>i;--j)
{
ans[j]='0';
}
puts(ans+1);
return 0;
}
}
printf("%s%s",ans+1,X+1);
// fclose(stdin);
// fclose(stdout);
return 0;
}
标解
毕姥爷太强了。
匹配字符串,考虑自动机上dp。
构造对x的KMP自动机,只是末尾位置的trans均指向末尾。
那么如果数字中出现过x,自动机上跑出来一定在末尾位置。
记\(f(i,j)\)表示从状态j走i步走到末尾位置上的方案数。写出转移方程:
\]
边界条件\(f(0,\textrm{maxstate})=1\)
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#pragma GCC optimize ("O0")
using namespace std;
template<class T> inline T read(T&x)
{
T data=0;
int w=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
return x=data*w;
}
typedef long long ll;
typedef unsigned long long ull;
const ll INF=(1ULL << 63) - 1;
const int MAXN=2e3+20;
char s[MAXN];
ll n;
int nx[MAXN];
int trans[MAXN][MAXN];
ull f[MAXN][MAXN];
int main()
{
freopen("python.in","r",stdin);
freopen("python.out","w",stdout);
scanf("%s",s+1);
int l=strlen(s+1);
// 处理next数组
int p=0;
for(int i=2;i<=l;++i)
{
while(p && s[p+1] != s[i])
{
p = nx[p];
}
if(s[p+1] == s[i])
{
++p;
}
nx[i]=p;
}
for(int i=0;i<l;++i) // 处理trans转移数组
for(int j=0;j<10;++j)
{
p = i;
while(p && s[p + 1] != j + '0')
{
p = nx[p];
}
if(s[p + 1] == j + '0')
{
++p;
}
trans[i][j] = p;
}
for(int i=0;i<10;++i)
{
trans[l][i]=l;
}
f[0][l]=1;
for(int i=1;i<2015;++i) // 2015是估计值
for(int j=0;j<=l;++j)
for(int k=0;k<10;++k)
{
f[i][j] += f[i-1][trans[j][k]];
if(f[i][j] > INF)
{
f[i][j] = INF;
}
}
read(n);
int lim;
for(lim=1;lim < 2015 && f[lim][0] < n;++lim);
p=0;
for(int i=lim,j;i;--i)
{
for(j=0;j<10;++j)
{
if(n > f[i-1][trans[p][j]])
n -= f[i-1][trans[p][j]];
else
break;
}
printf("%d",j);
p = trans[p][j];
}
puts("");
// fclose(stdin);
// fclose(stdout);
return 0;
}
test20181016 B君的第一题的更多相关文章
- test20181017 B君的第一题
题意 分析 考场做法 对p的幂打表发现,我们一定可以把x和y的二进制位从低到高依次调整成0. 具体而言,从0次幂开始每两个分为一组a,b,那么0,a,b,a+b组合中的一种可以将x,y的对应二进制位都 ...
- test20181016 B君的第二题
题意 分析 考场暴力50分. 考虑bfs序,一个点的儿子节点的bfs序一定连续,所以对bfs序建线段树,努力打一下就行了. 时间复杂度\(O(n \log n + m \log n)\) #inclu ...
- test20181018 B君的第一题
题意 分析 考场爆零做法 考虑dp,用\(f(i,j,0/1)\)表示i及其子树中形成j个边连通块的方案数,其中i是否向外连边. \(O(n^3)\),转移方程太复杂就打挂了. #include< ...
- test20181020 B君的第一题
题意 分析 二次剩余问题. x,y相当于二次方程 \[ x^2-bx+c=0 \mod{p} \] 的两根. 摸意义下的二次方程仍然考虑判别式\(\Delta=b^2-4c\). 它能开根的条件是\( ...
- test20181019 B君的第一题
题意 分析 考场做法同标解. 画图模拟分析发现,无论操作顺序怎样,操作数的奇偶性是不变的. 所以等同求出,以每点为根的操作数奇偶性. 用\(f(x)\)表示x及其子树中的边,包括x到它fa的边,将他们 ...
- [算法 笔记]2014年去哪儿网 开发笔试(续)第一题BUG修正
上一篇的blog地址为:http://www.cnblogs.com/life91/p/3313868.html 这几天又参加了一个家公司的笔试题,在最后的编程题中竟然出现了去哪儿网开发的第一题,也就 ...
- 《学习OpenCV》练习题第五章第一题ab
这道题是载入一幅带有有趣纹理的图像并用不同的模板(窗口,核)大小做高斯模糊(高斯平滑),然后比较用5*5大小的窗口平滑图像两次和用11*11大小的窗口平滑图像一次是否接近相同. 先说下我的做法,a部分 ...
- 《学习OpenCV》练习题第四章第一题b&c
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...
- 《学习OpenCV》练习题第四章第一题a
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...
随机推荐
- English trip -- VC(情景课)4 D
What do you see? I can see three men in the pictrue. one older man is a doctor, two younger men are ...
- LeetCode--202--快乐数
问题描述: 编写一个算法来判断一个数是不是“快乐数”. 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变 ...
- 当保存在Session中的对象,取出后,在外部发生改变时会怎样
return_reason_model model = new return_reason_model(); model.F_RetunrnReason = "; HttpContext.S ...
- Linux的三种网络适配器
Linux的三种网络适配器 分别为:桥接模式(Bridged),NAT模式,仅主机模式. 仅主机模式: 2>NAT模式 NAT 是虚拟机和本地网络使用一个ip地址 3>桥接模 ...
- entest1
1◆ ai I 2◆ ai I ir ɜː ie i: 3◆ u: ʌ ɜː ə ui u: ure ʊə
- poj 2777线段树应用
敲了n遍....RE愉快的debug了一晚上...发现把#define maxn = 100000 + 10 改成 #define maxn = 100010 就过了....感受一下我呵呵哒的表情.. ...
- UVALive 5881
DES:给出一个数列.然后有q个询问,对给定的区间内是否有重复的数.如果有输出任意一个重复的.如果没有输出OK. 开始以为是线段树.后来发现.只是水的比较隐蔽.感觉这个方法还是很聪明的.把每个点的最近 ...
- 快速切题 sgu105. Div 3 数学归纳 数位+整除 难度:0
105. Div 3 time limit per test: 0.25 sec. memory limit per test: 4096 KB There is sequence 1, 12, 12 ...
- bzoj1677
题解: 背包 每一个1<<i都是无限量 代码: #include<bits/stdc++.h> using namespace std; ,M=1e9; int n,dp[N] ...
- Java——使用File类递归遍历指定路劲下的所有文件
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...