码迷,mamicode.com
首页 > 其他好文 > 详细

codechef Count Relations(组合数 二项式定理)

时间:2018-09-06 18:10:02      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:等于   turn   eve   play   mod   暴力   include   组合数   src   

题意

求有多少元素属于$1 \sim N$的集合满足

R1 = {(x,y):x和y属于B,x不是y的子集,y不是x的子集,x和y的交集等于空集}

R2 = {(x,y):x和y属于B,x不是y的子集,y不是x的子集,x和y的交集不等于空集}

Sol

神仙题啊Orz

我整整推了两个小时才推出来

首先写暴力

技术分享图片
/*
*/
#include<iostream>
#include<cstdio>
#include<cstring>
//#define int long long 
#define LL int
const int MAXN = 1001;
using namespace std;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < 0 || c > 9) {if(c == -) f = -1; c = getchar();}
    while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = getchar();
    return x * f;
}
int N;
int C[MAXN][MAXN], Po2[MAXN];
main() {
    cin >> N;
    Po2[0] = 1;
    for(int i = 1; i <= 1000; i++) Po2[i] = 2 * Po2[i - 1];
    C[0][0] = 1;
    for(int i = 1; i <= 1000; i++) {
        C[i][0] = 1;
        for(int j = 1; j <= i; j++)
            C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; 
    }
    int ans = 0;
    for(int i = 1; i <= N - 1; i++) {
        int now = C[N][i], sum = 0;
        for(int j = 1; j <= N - i; j++)    sum += C[N - i][j];
        //ans += now * sum;
        ans += now * (Po2[N - i] - 1);
    }
    cout << ans / 2 << " ";
    ans = 0;
    for(int i = 2; i <= N - 1; i++) {
        int res1 = C[N][i], sum1 = 0;
        for(int j = 1; j <= i - 1; j++) {
            int res2 = C[i][j], sum2 = 0;
            for(int k = 1; k <= N - i; k++) {
                sum2 += C[N - i][k];
            }
            sum1 += res2 * sum2;
        }
        ans += res1 * sum1;
    }
    cout << ans / 2;
    return 0;
}
/*
100 50 10000006
*/
暴力

然后一层一层展开即可

最后的答案为

第一问:

$$\frac{3^n + 1}{2} - 2^n$$‘

第二问:

$$\frac{-3 * 3^n + 4^n - 1}{2} + 3 * 2^{n - 1}$$

/*
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define int long long 
#define LL int
const int MAXN = 1001, mod = 100000007, inv = 50000004;
using namespace std;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < 0 || c > 9) {if(c == -) f = -1; c = getchar();}
    while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = getchar();
    return x * f;
}
int N;
int C[MAXN][MAXN], Po2[MAXN], Po3[MAXN], Po4[MAXN];
int f(int a, int p) {
    int base = 1;
    while(p) {
        if(p & 1) base = (base * a) % mod;
        a = (a * a) % mod; p >>= 1;
    }
    return base % mod;
}
main() {
    int T;
    cin >> T;
    while(T--) {
        cin >> N;
        /*Po2[0] = Po3[0] = Po4[0] = 1; 
        for(int i = 1; i <= 1000; i++) Po2[i] = 2 * Po2[i - 1], Po3[i] = 3 * Po3[i - 1], Po4[i] = 4 * Po4[i - 1];
        int ans = (Po3[N] + 1) / 2 - Po2[N];
        cout << ans << " ";
        ans = 0;
        ans = (-3 * Po3[N] + Po4[N] - 1) / 2;
        ans += Po2[N - 1] * 3;
        cout << ans;    */
        //cout << f(2, mod - 2) % mod;
        int ans = ((f(3, N) + 1) * inv - f(2, N) + mod) % mod;
        cout << ans << " ";
        ans = ((-3 * f(3, N) + mod) % mod + f(4, N) - 1 + mod) * inv + f(2, N - 1) * 3 % mod;
        ans %= mod;
        cout << ans << endl;
    }

    return 0;
}
/*
*/

codechef Count Relations(组合数 二项式定理)

标签:等于   turn   eve   play   mod   暴力   include   组合数   src   

原文地址:https://www.cnblogs.com/zwfymqz/p/9599352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!