牛客竞赛——组合数问题

 

组合数数学公式:c[i][j]=c[i-1][j]+c[i-1][j-1],特殊c[0][0]=1; 

 此题如果直接暴力超时,需要预处理,求一个前缀和。。。。。。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
ll c[2010][2010];
//组合数数学公式c[i][j]=c[i-1][j-1]+c[i-1][j]; 
void init(int k){
	c[0][0]=1%k;
	for(int i=1;i<=2005;i++){
		c[i][0]=c[i][i]=1%k;
		for(int j=1;j<i;j++){
			c[i][j]=(c[i-1][j-1]+c[i-1][j])%k;
		}
	}
	//判断每一对i,j是否是k的倍数 
	for(int i=0;i<=2000;i++){
		for(int j=0;j<=i;j++){
			c[i][j]=c[i][j]==0?1:0;
		}
	}
	for(int i=0;i<=2000;i++){
		for(int j=1;j<=i;j++){
			c[i][j]+=c[i][j-1];//处理前缀和 
		}
	}
}

int main(){
	ll t,k,n,m;
	cin>>t>>k;
	init(k);
	while(t--){
		cin>>n>>m;
		ll cnt=0;
		for(ll i=n;i>=0;i--){
			cnt+=c[i][min(i,m)];
		}
		cout<<cnt<<endl;
	}
	return 0;
}
//            /\       |  /  |**、
//			 /  \      | /   |   \
//			/    \     |/    |   /  _____                      ____   |  /
//		   /------\    |\    |__/  /     \  \      /\      /  /    \  | /
//		  /        \   | \   |    /       \  \    /  \    /  /______\ |/
//		 /          \  |  \  |    \       /   \  /    \  /   \        |
//      /            \ |   \ |     \_____/     \/      \/     \_____  |
/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
发布了200 篇原创文章 · 获赞 38 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43746332/article/details/105010747