1. 程式人生 > >洛谷 P4317 花神的數論題(組合數)

洛谷 P4317 花神的數論題(組合數)

題面

luogu

題解

組合數

列舉有多少個\(1\),求出有多少種數
掃描\(n\)的每一位\(1\), 強制選\(0\)然後組合數算一下有多少種方案

Code

#include<bits/stdc++.h>

#define LL long long
#define RG register

using namespace std;
template<class T> inline void read(T &x) {
    x = 0; RG char c = getchar(); bool f = 0;
    while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
    while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
    x = f ? -x : x;
    return ;
}
template<class T> inline void write(T x) {
    if (!x) {putchar(48);return ;}
    if (x < 0) x = -x, putchar('-');
    int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
    for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
}
const int N = 55, Mod = 10000007;
LL C[N][N], cnt, a[N];
inline LL Pow(int a, LL b) {
    LL s = 1;
    for (LL x = a; b; b >>= 1, x = x*x%Mod) if (b&1) s = s*x%Mod;
    return s;
}
int main() {
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    for (int i = 0; i <= 50; i++) C[i][i] = C[i][0] = 1;
    for (int i = 2; i <= 50; i++)
        for (int j = 1; j < i; j++)
            C[i][j] = C[i-1][j-1]+C[i-1][j];
    LL n; read(n);
    for (int i = 50; i >= 0; i--) {
        if ((n>>i)&1) {
            for (int j = 1; j <= i; j++)
                a[j+cnt] += C[i][j];//至少選1個的方案
            a[++cnt]++;//不選的方案(必須分開算,不然會有重複計算)
        }
    }
    LL ans = 1;
    for (int i = 1; i <= 50; i++)
        ans = ans*Pow(i, a[i])%Mod;
    printf("%lld\n", ans);
    return 0;
}