1. 程式人生 > >BZOJ2425 [HAOI2010]計數 【數位dp】

BZOJ2425 [HAOI2010]計數 【數位dp】

cpp urn 所有 namespace HA div n) != haoi2010

題目

你有一組非零數字(不一定唯一),你可以在其中插入任意個0,這樣就可以產生無限個數。比如說給定{1,2},那麽可以生成數字12,21,102,120,201,210,1002,1020,等等。

現在給定一個數,問在這個數之前有多少個數。(註意這個數不會有前導0).

輸入格式

只有1行,為1個整數n.

輸出格式

只有整數,表示N之前出現的數的個數。

輸入樣例

1020

輸出樣例

7

提示

n的長度不超過50,答案不超過\(2^{63}-1\).

題解

如果我們看做把0刪除看做把0前導,那麽問題就轉化成了求所有數的排列中比當前數小的個數
我們只需統計當前\(i\)位相同,第\(i + 1\)位比原數小時有多少種情況
那麽剩余的位就可以隨便排列了,用帶重復元素的排列\(\frac{N!}{n1!*n2!*n3!......}\)


當然可能會爆long long,可以對階乘質因子分解來計算

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<‘ ‘; puts("");
using namespace std; const int maxn = 55,maxm = 100005,INF = 1000000000; inline int read(){ int out = 0,flag = 1; char c = getchar(); while (c < 48 || c > 57){if (c == ‘-‘) flag = -1; c = getchar();} while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();} return
out * flag; } int num[maxn],n,isn[maxn]; LL a[10],fac[maxn],p[maxn],pi,ans; void init(){ for (int i = 2; i < maxn; i++){ if (!isn[i]) p[++pi] = i; for (int j = 1; j <= pi && i * p[j] < maxn; j++){ isn[i * p[j]] = true; if (i % p[j] == 0) break; } } } LL Cal(LL x,LL t){ LL re = 0; while (x / t) re += (x /= t); return re; } LL qpow(LL a,LL b){ LL re = 1; for (; b; b >>= 1,a = a * a) if (b & 1) re = re * a; return re; } LL cal(){ LL re = 1,tot = 0; for (int i = 0; i < 10; i++) tot += a[i]; for (int i = 1; i <= pi && p[i] <= tot; i++){ LL cnt = Cal(tot,p[i]); for (int j = 0; j < 10; j++) cnt -= Cal(a[j],p[i]); re = re * qpow(p[i],cnt); } return re; } int main(){ init(); char c; while ((c = getchar()) != EOF){ if (!isdigit(c)) break; num[++n] = c - ‘0‘; a[num[n]]++; } for (int i = 1; i <= n; i++){ for (int j = 0; j < num[i]; j++){ if (!a[j]) continue; a[j]--; ans += cal(); a[j]++; } a[num[i]]--; } cout << ans << endl; return 0; }

BZOJ2425 [HAOI2010]計數 【數位dp】