没过样例求助
查看原帖
没过样例求助
1296840
zhang_fengrui楼主2024/4/20 14:17

rt,求助各位

#include <cstdio>
#include <cstring>
#define int long long
using namespace std;
int L,R;
int dp[15][15][15][2][2][2];//第i位,是否超界,是否有前导0
void init(){
	memset(dp,0,sizeof(dp));
	for(int j=1;j<=9;j++){
		for(int k=0;k<=9;k++){
			for(int b1=0;b1<=1;b1++){
				for(int b2=0;b2<=1;b2++){
					for(int b3=0;b3<=1;b3++){
						if(!b1){
							if(k==4){
								continue;
							}
							if(j==4){
								continue;
							}
						}
						else{
							if(k!=4 && j!=4){
								continue;
							}
						}
						if(!b2){
							if(k==8){
								continue;
							}
							if(j==8){
								continue;
							}
						}
						else{
							if(k!=8 && j!=8){
								continue;
							}
						}
						if(b3){
							continue;
						}
						dp[2][j][k][b1][b2][b3]=1;
					}
				}
			}
		}
	}
}
void mj(){
	for(int i=3;i<=10;i++){
		for(int j=0;j<=9;j++){
			for(int k=0;k<=9;k++){
				for(int b1=0;b1<=1;b1++){
					for(int b2=0;b2<=1;b2++){
						for(int b3=0;b3<=1;b3++){
							for(int a=0;a<=9;a++){
								if(j==k && a==j && !b3){
									continue;
								}
								if((j==4 || k==4 || a==4)&& !b1){
									continue;
								}
								if((j==8 || k==8 || a==8)&& !b2){
									continue;
								}
								dp[i][j][k][b1][b2][b3]+=dp[i-1][k][a][b1][b2][b3];
								if(b1){
									if(j==4){
										dp[i][j][k][b1][b2][b3]+=dp[i-1][k][a][0][b2][b3];
									}
								}
								if(b2){
									if(j==8){
										dp[i][j][k][b1][b2][b3]+=dp[i-1][k][a][b1][0][b3];
									}
								}
								if(b3){
									if(a==j && j==k){
										dp[i][j][k][b1][b2][b3]+=dp[i-1][k][a][b1][b2][0];
									}
								}
							}
						} 
					}
				}
			}
		}
	}
}
int La[20],Ra[20]; 
signed main(){
	scanf("%lld%lld",&L,&R);
	init();
	mj();
	int ans=0;
	for(int i=1,j=L;j;i++,j/=10){
		La[i]=j%10;
	}
	for(int i=1,j=R;j;i++,j/=10){
		Ra[i]=j%10;
	}
	for(int j=La[10];j<=Ra[10];j++){
		for(int k=0;k<=9;k++){
			if(j==Ra[10] && k>Ra[9]){
				break;
			}
			if(j==La[10] && k<La[9]){
				continue;
			}
			ans+=dp[10][j][k][1][0][1];
			ans+=dp[10][j][k][0][1][1];
			ans+=dp[10][j][k][0][0][1];
		}
	}
	for(int j=La[8];j<=Ra[8];j++){
		for(int k=0;k<=9;k++){
			if(j==Ra[8] && k<Ra[7]){
				break;
			}
			if(j==La[8] && k>La[7]){
				continue;
			}
			ans-=dp[8][j][k][1][0][1];
			ans-=dp[8][j][k][0][1][1];
			ans-=dp[8][j][k][0][0][1];
		}
	}
	for(int j=La[6];j<=Ra[6];j++){
		for(int k=0;k<=9;k++){
			if(j==Ra[6] && k>Ra[5]){
				break;
			}
			if(j==La[6] && k<La[5]){
				continue;
			}
			ans+=dp[6][j][k][1][0][1];
			ans+=dp[6][j][k][0][1][1];
			ans+=dp[6][j][k][0][0][1];
		}
	}
	for(int j=La[4];j<=Ra[4];j++){
		for(int k=0;k<=9;k++){
			if(j==Ra[4] && k<Ra[3]){
				break;
			}
			if(j==La[4] && k>La[3]){
				continue;
			}
			ans-=dp[4][j][k][1][0][1];
			ans-=dp[4][j][k][0][1][1];
			ans-=dp[4][j][k][0][0][1];
		}
	}
	for(int j=La[2];j<=Ra[2];j++){
		for(int k=0;k<=9;k++){
			if(j==Ra[2] && k<Ra[1]){
				break;
			}
			if(j==La[2] && k>La[1]){
				continue;
			}
			ans+=dp[2][j][k][1][0][1];
			ans+=dp[2][j][k][0][1][1];
			ans+=dp[2][j][k][0][0][1];
		}
	}
	printf("%lld",ans);
	return 0;
}
//0
//4482343
//4482343
//4482542
//4482542
//4482542
2024/4/20 14:17
加载中...