跳到正文

个人记录

代码分析0x0c

hyk0YF0Gamer约 4 分钟
原文
文章信息与版本

文章信息

发布
2026年9月6日
收录
2026年9月7日
更新
2026年11月8日
篇幅
约 4 分钟

版本

  1. v1当前

https://www.luogu.com.cn/article/bvjysv1a

P3303

C++
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define For(i, a, b) for (i = a; i <= b; i++)
using namespace std;
typedef long long ll;
const int N = 15, M = 15000, MX = 1e9 + 7;
ll n, otz[M], f[N][M][2], sum[M];
int K, QAQ, a[N], ans;
map<ll, int> orz;
struct cyx
{
	int id, pos;
	cyx() {}
	cyx(int _x, int _y) :
		id(_x), pos(_y) {}
	friend inline bool operator < (cyx a, cyx b)
	{
		return sum[a.id] * sum[a.pos] < sum[b.id] * sum[b.pos];
	}
};
priority_queue<cyx> pq;
void DP(ll num)
{
	int i, j, k, n = 0;
	while (num) a[++n] = num % 10, num /= 10;//分解数位 
	For (k, 1, 9)//初始化 
		f[1][orz[k]][k > a[1]]++;
	For (i, 2, n) For (j, 1, QAQ) For (k, 1, 9)
	{
		ll q = otz[j]; if (q % k != 0) continue;
		int h = orz[q / k];//计算从何处进行转移 
		if (k < a[i]) f[i][j][0] += f[i - 1][h][0] + f[i - 1][h][1];
		else if (k > a[i])//进行转移 
			f[i][j][1] += f[i - 1][h][0] + f[i - 1][h][1];
		else f[i][j][0] += f[i - 1][h][0],
			f[i][j][1] += f[i - 1][h][1];
	}
	For (j, 1, QAQ)	For (i, 1, n)
		sum[j] += f[i][j][0] + (i == n ? 0 : f[i][j][1]);
	sort(sum + 1, sum + QAQ + 1);
	K = min(1ll * K, 1ll * QAQ * QAQ);
}
int main()
{
	int i, j, k, h;
	ll x = 1;
	cin >> n >> K;
	For (i, 0, 39)
	{
		ll t = x;
		For (j, 0, 25)
		{
			ll r = x;
			For (k, 0, 17)
			{
				ll w = x;
				For (h, 0, 14)
				{
					otz[orz[x] = ++QAQ] = x;
					x *= 7;//一直到这里都是枚举因子 
					if (x > n) break;
				}
				x = w * 5;
				if (x > n) break;
			}
			x = r * 3;
			if (x > n) break;
		}
		x = t * 2;
		if (x > n) break;
	}
	DP(n);//进行dp转移 
	For (i, 1, QAQ) pq.push(cyx(i, QAQ));
	For (i, 1, K)//求出最高的K个位置作为答案 
	{
		cyx u = pq.top(); pq.pop();
		ans = (ans + sum[u.id] * sum[u.pos] % MX) % MX;
		if (u.pos == 1) continue;
		pq.push(cyx(u.id, u.pos - 1));
	}
	cout << ans << endl;
	return 0;
}
原文