zoukankan      html  css  js  c++  java
  • 洛谷P4492 [HAOI2018]苹果树(组合数)

    题意

    题目链接

    Sol

    有点自闭,。我好像对组合数一窍不通(~~~~)

    Orz shadowice

    // luogu-judger-enable-o2
    #include<bits/stdc++.h>
    #define LL long long 
    int mod;
    template<typename A, typename B> inline bool chmax(A &x, B y) {return x < y ? x = y, 1 : 0;}
    template<typename A, typename B> inline bool chmin(A &x, B y) {return x > y ? x = y, 1 : 0;}
    template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
    template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
    template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
    template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
    #define LL long long 
    using namespace std;
    const int MAXN = 2001, INF = 1e9 + 10;
    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;
    }
    LL N, fac[MAXN], C[MAXN][MAXN];
    signed main() {
    	N = read(); mod = read();
    	fac[0] = 1;
    	for(int i = 1; i <= N; i++) fac[i] = mul(i, fac[i - 1]);
    	for(int i = 0; i <= N; i++) {
    		C[i][0] = C[i][i] = 1;
    		for(int j = 1; j < i; j++) C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]);
    	}
    	LL res = 0;
    	for(int i = 2; i <= N; i++) {
    		LL ans = 0;
    		for(int j = 1; j <= N - i + 1; j++) {
    			add2(ans, 1ll * fac[j] % mod * C[N - i][j - 1] % mod * j % mod * (N - j) % mod * fac[N - j - 1] % mod * i % mod * (i - 1) % mod);
    		}
    		add2(res, ans);
    	}
    	cout << res;
        return 0;
    }
    /*
    6
    2 5 2 3 5 4
    1
    1
    1
    2
    4
    */
    
  • 相关阅读:
    百度网盘下载太慢,试试阿里云网盘?
    linux使用过程中遇到的常见问题
    vscode设置护眼色
    为什么程序员互相之间不能透露薪水?
    机器学习:支持向量机(SVM)
    Java多线程总结(三)
    Java多线程总结(二)
    Java多线程总结(一)
    8.多线程--避免活跃性危险
    7.多线程--线程池的使用
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10450771.html
Copyright © 2011-2022 走看看