luogu P2481 [SDOI2010]代码拍卖会

luogu P2481 [SDOI2010]代码拍卖会

题目大意

就是求长度为 n n n的数,每一位不比前一位小,且可以被P整除的数的个数

题解

首先这题要用到一个非常有趣的转换

假设有 n n n

则问题可以转换为

1 , 11 , 111 , 1111 , 11111 , . . . . . , 111...1 ( n 个 1 ) 1, 11, 111, 1111, 11111, ....., 111...1(n个1) 1,11,111,1111,11111,.....,111...1(n1)
在上面这 n n n个数中取不超过 9 9 9个的方案数(可以重复取)

发现就是一个很简单的组合数,假设取 k k k个,则 方 案 数 是 C n + k − 1 k 方案数是 C_{n+k-1}^k Cn+k1k

发现 P P P很小

然后预处理出每个余数在上述序列中有几个

最后就是简单的DP了

#include<bits/stdc++.h>
#define int long long
#define P 999911659
#define N 505
using namespace std;
int inv[N];
int qpow(int x, int y){
	int ret = 1;
	for(; y; y >>= 1, x = x * x % P) if(y & 1) ret = ret * x % P;
	return ret;
}
int C(int x, int y){
	int ret = 1;
	for(int i = 1; i <= y; i ++) ret = ret * (x - i + 1) % P * inv[i] % P; 
	return ret;
}
int n, mod, a[N], f[2][15][N], cnt[N], m;
signed main(){
	scanf("%lld%lld", &n, &mod);
	for(int i = 1; i <= 8; i ++) inv[i] = qpow(i, P - 2);
	int p = 0, pos = 1e18;
	m = 1e18;
	int now = 1e18;
	for(int i = 1; i <= n; i ++){
		p = (p * 10 + 1) % mod;
		if(i == n) now = p;
		if(a[p]){
			m = i - a[p];
			pos = a[p];
			break;
		}
		a[p] = i; cnt[p] ++;
	}
	for(int i = 0; i < mod; i ++)
		if(a[i] >= pos) cnt[i] = ((n - a[i]) / m + 1) % P;
		
	if(now == (1e18))
	for(int i = 0; i < mod; i ++){
		if(a[i] >= pos && (n  - a[i]) % m == 0) now = i;
	}
	f[0][0][now] = 1;
	int fo = 0, to = 1, ans = 0;
	for(int j = 0; j < mod; j ++, swap(fo, to)){ 
		for(int i = 0; i < mod; i ++)
				for(int gs = 0; gs <= 8; gs ++) f[to][gs][i] = f[fo][gs][i];
		if(cnt[j] == 0) continue;
			for(int i = 0; i < mod; i ++)
				for(int gs = 1; gs <= 8; gs ++)
					for(int k = 1; k <= gs; k ++)
						f[to][gs][(i + k * j) % mod] = (f[to][gs][(i + k * j) % mod] + f[fo][gs - k][i] * C(cnt[j] + k - 1, k) % P) % P;
	}
	for(int i = 0; i < 9; i ++) ans = (ans + f[fo][i][0]) % P;
	printf("%lld", ans);
	return 0;
} 

坑点

  • cnt的处理,循环节的处理
  • 原序列没有前导0,所以 111...1 ( n 个 1 ) 111...1(n个1) 111...1(n1必选
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值