记忆化全RE求助
查看原帖
记忆化全RE求助
443675
紊莫turtle楼主2022/10/1 14:11

本地跑全RE,dfs函数中,n是当前数字的长度,pos当前位置,last是上一个的位置,flag表示能填0-9还是0-某一位,f0表示有没有前导0。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define debug puts("Fuck shit");
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void write(int x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}
inline void writeln(int x){write(x);putchar('\n');}
inline void writesp(int x){write(x);putchar(' ');}
int p[15],f[15][15][2][2],lena,lenb,la,lb;
int len(int x)
{
	int rt=0;while(x){rt++,x/=10;}return rt;
}
int dfs(int n,int pos,int last,int flag,int f0)
{
	if(pos>n) return 1;
	if(f[pos][last][flag][f0]) return f[pos][last][flag][f0];
	for(int i=0;i<=(flag?p[pos]:9);i++)
	{
		if(abs(i-last)<2) continue;
		if((f0&&i==0))
			f[pos][last][flag][f0]+=dfs(n,pos+1,-100,0,1);
		else
			f[pos][last][flag][f0]+=dfs(n,pos+1,i,(flag&&i==p[pos]),0);
	}
	return f[pos][last][flag][f0];
}
signed main()
{
//	debug
    int a=1,b=10;
	lena=len(a),lenb=len(b),la=lena,lb=lenb;
	while(a)
		p[la--]=a%10,a/=10;
	int x=dfs(lena,1,-100,1,1);
//	debug
	memset(p,0,sizeof p);
	memset(f,0,sizeof f);
	while(b)
		p[lb--]=b%10,b/=10;
	int y=dfs(lenb,1,-100,1,1);
	cout<<x;
    return 0;
}
2022/10/1 14:11
加载中...