BZOJ 1974: [Sdoi2010]auction 代码拍卖会( dp )

在1, 11, 111……中选<=8个, + 11..(n个1)拼出所有可能...这些数mod p至多有p中可能, 找出循环的处理一下. 那么dp就很显然了...dp(i, j, k)表示前i种选出了j个, 组合出的数mod p = k, 然后递推一下就好了. 

-----------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
typedef long long ll;
 
const int maxn = 509;
const int maxm = 1000;
const int MOD = 999911659;
const int n = 9;
 
int dp[2][maxm][maxn], Inv[maxm], C[maxn][maxm], cir[maxn];
int P, R;
ll N, cnt[maxn];
 
void Gcd(int a, int b, int &d, ll &x, ll &y) {
if(!b) {
d = a;
x = 1;
y = 1;
} else {
Gcd(b, a % b, d, y, x);
y -= x * (a / b);
}
}
 
int _C(ll n, int m) {
if(m > n)
return 0;
int ret = 1;
for(ll x = n - m + 1; x <= n; x++)
ret = ll(ret) * (x % MOD) % MOD;
return ll(ret) * Inv[m] % MOD;
}
 
void Init() {
memset(cnt, 0, sizeof cnt);
scanf("%lld%d", &N, &P);
int x = 1 % P, sz = 0;
while(!cnt[x]) {
cir[cnt[x] = ++sz] = x;
if(sz >= N)
break;
x = (x * 10 + 1) % P;
}
if(sz != N) {
ll _N = N - cnt[x] + 1;
int  _sz = sz - cnt[x] + 1;
if(_sz > 1)
R = P - cir[cnt[x] + (_N % _sz ? _N % _sz : _sz) - 1];
else
R = P - cir[cnt[x]];
for(int i = 0, t = cnt[x]; i < P; i++) if(cnt[i]) {
if(cnt[i] < t)
cnt[i] = 1;
else
cnt[i] = _N / _sz + (_sz > 1 && (_N % _sz) > cnt[i] - t);
}
} else {
R = P - x;
for(int i = 0; i < P; i++)
if(cnt[i]) cnt[i] = 1;
}
if(R >= P)
R -= P;
for(int i = 0, fac = 1; i < n; i++, fac = i * fac % MOD) {
int d;
ll x, y;
Gcd(fac, MOD, d, x, y);
Inv[i] = (x + MOD) % MOD;
}
for(int i = 0; i < P; i++)
for(int j = 0; j < n; j++)
if(cnt[i]) C[i][j] = _C(cnt[i] + j - 1, j);
}
 
inline void upd(int &x, int t) {
if((x += t) >= MOD)
x -= MOD;
}
 
int main() {
Init();
int c = 0, p = 1;
dp[c][0][0] = 1;
for(int i = 0; i < P; i++) if(cnt[i]) {
swap(c, p);
for(int j = 0; j < n; j++)
for(int k = 0; k < P; k++)
dp[c][j][k] = dp[p][j][k];
for(int j = 0; j < n; j++)
for(int k = 0; k < P; k++) if(dp[p][j][k])
for(int t = 1; t + j < n; t++)
upd(dp[c][j + t][(k + t * i) % P], ll(dp[p][j][k]) * C[i][t] % MOD);
}
int ans = 0;
for(int i = 0; i < n; i++)
upd(ans, dp[c][i][R]);
printf("%d ", ans);
return 0;
}

-----------------------------------------------------------------------

1974: [Sdoi2010]auction 代码拍卖会

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 263  Solved: 97
[Submit][Status][Discuss]

Description

随着iPig在P++语言上的造诣日益提升,他形成了自己一套完整的代码库。猪王国想参加POI的童鞋们都争先恐后问iPig索要代码库。iPig不想把代码库给所有想要的小猪,只想给其中的一部分既关系好又肯出钱的小猪,于是他决定举行了一个超大型拍卖会。 在拍卖会上,所有的N头小猪将会按照和iPig的好感度从低到高,从左到右地在iPig面前站成一排。每个小猪身上都有9猪币(与人民币汇率不明),从最左边开始,每个小猪依次举起一块牌子,上面写上想付出的买代码库的猪币数量(1到9之间的一个整数)。大家都知道,如果自己付的钱比左边的猪少,肯定得不到梦寐以求的代码库,因此从第二只起,每只猪出的钱都大于等于左边猪出的价钱。最终出的钱最多的小猪(们)会得到iPig的代码库真传,向着保送PKU(Pig Kingdom University)的梦想前进。 iPig对自己想到的这个点子感到十分满意,在去现场的路上,iPig就在想象拍卖会上会出现的场景,例如一共会出现多少种出价情况之类的问题,但这些问题都太简单了,iPig早已不敢兴趣了,他想要去研究更加困难的问题。iPig发现如果他从台上往下看,所有小猪举的牌子从左到右将会正好构成一个N位的整数,他现在想要挑战的问题是所有可能构成的整数中能正好被P整除的有多少个。由于答案过大,他只想要知道答案mod 999911659就行了。

Input

有且仅有一行:两个数N(1≤N≤10^18)、P(1≤P≤500),用一个空格分开。

Output

有且仅有一行:一个数,表示答案除以999911659的余数。

Sample Input

2 3

Sample Output

15
样例解释
方案可以是:12 15 18 24 27 33 36 39 45 48 57 66 69 78 99,共15种。
数据规模
测试点 N P 测试点 N P
1 ≤1000 ≤500 6 ≤10^6 ≤500
2 ≤10^18 5 7 ≤10^18 ≤120
3 ≤10^18 ≤10 8 ≤10^18 ≤500
4 ≤10^18 ≤10 9 ≤10^18 ≤500
5 ≤10^18 25 10 ≤10^18 ≤500

HINT

Source

原文地址:https://www.cnblogs.com/JSZX11556/p/5100273.html