求助 3个样例全过 0pts
查看原帖
求助 3个样例全过 0pts
520777
LaoXu666楼主2023/12/24 11:14

rt 求调

#include <cstring>
#include <iostream>

typedef long long LL;
LL L, R;
int Cnt[4];
int Id[4] = {2, 3, 5, 7};
int Divide[15][5] = {{},
					 {},
					 {1},
					 {0, 1},
					 {2},
					 {0, 0, 1},
					 {1, 1},
					 {0, 0, 0, 1},
					 {3},
					 {0, 2}};
LL Base[25], L2, R2, DP[20][40][30][20][15], Ans;

LL DFS(int X, LL Num) {
	if (Cnt[0] < 0 || Cnt[1] < 0 || Cnt[2] < 0 || Cnt[3] < 0) return 0;
	LL ML = Num * Base[18 - X], MR = ML + Base[18 - X] - 1;
	if (ML > R2 || MR < L2) {
		return 0;
	}
	if (X >= 18) {
		return !(Cnt[0] || Cnt[1] || Cnt[2] || Cnt[3]);
	}
	bool Limit = (ML >= L2 && MR <= R2);
	if (DP[X][Cnt[0]][Cnt[1]][Cnt[2]][Cnt[3]] != -1 && Limit) return DP[X][Cnt[0]][Cnt[1]][Cnt[2]][Cnt[3]];
	LL Ans4 = 0;
	for (int i = (Num > 0); i <= 9; i++) {
		for (int j = 0; j < 4; j++) {
			Cnt[j] -= Divide[i][j];
		}
		Ans += DFS(X + 1, Num * 10 + i);
		for (int j = 0; j < 4; j++) {
			Cnt[j] += Divide[i][j];
		}
	}
	if (Limit) DP[X][Cnt[0]][Cnt[1]][Cnt[2]][Cnt[3]] = Ans4;
	return Ans4;
}

void Prev(int Step, LL Mul) {
	if (Mul * Mul > R) {
		return;
	}
	if (Step == 4) {
		L2 = (L - 1) / Mul + 1;
		R2 = R / Mul;
		Ans += DFS(0, 0);
		return;
	}
	Prev(Step + 1, Mul);
	Cnt[Step]++;
	Prev(Step, Mul * Id[Step]);
	Cnt[Step]--;
}

int main() {
	std::cin >> L >> R;
	Base[0] = 1;
	for (int i = 1; i < 20; i++) {
		Base[i] = Base[i - 1] * 10;
	}
	std::memset(DP, -1, sizeof DP);
	Prev(0, 1);
	std::cout << Ans << '\n';
	return 0;
}
2023/12/24 11:14
加载中...