传送门
http://www.lydsy.com/JudgeOnline/problem.php?id=3209
Solution
如果 N=2x−1 ,明显答案就是 Πxi=1iCix 。现在 N 取任何数,我们不能直接用组合数学的公式,没事,我们用数位DP来推。
设
这题我犯了一个很愚蠢的错误,我将
f
数组取模了,然而指数是不能先取模的!于是只在最后取模,将
Code
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define maxn 105
#define MOD 10000007
using namespace std;
long long num;
int n, a[maxn], ans;
long long f[maxn][2][maxn];
int Pow(int x, long long y){
int res = 1;
while(y){
if(y & 1LL) res = 1LL * res * x % MOD;
x = 1LL * x * x % MOD;
y >>= 1LL;
}
return res;
}
int main(){
scanf("%lld", &num);
while(num){
a[++n] = num & 1LL;
num >>= 1LL;
}
for(int i = 1; i <= (n>>1); i++) swap(a[i], a[n-i+1]);
f[0][1][0] = 1LL;
for(int i = 0; i < n; i++)
for(int lim = 0; lim < 2; lim++){
int up = lim ? a[i+1] : 1;
for(int j = 0; j < n; j++){
if(!f[i][lim][j]) continue;
for(int k = 0; k <= up; k++){
int nlim = (a[i+1] == k && lim), nj = j + (k == 1);
f[i+1][nlim][nj] += f[i][lim][j];
}
}
}
ans = 1;
for(int lim = 0; lim < 2; lim++)
for(int j = 1; j <= n; j++)
ans = 1LL * ans * Pow(j, f[n][lim][j]) % MOD;
printf("%d\n", ans);
return 0;
}