洛谷P2822组合数问题
传送门啦
15分暴力,但看题解说暴力分有30分。
就是找到公式,然后套公式。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
long long read(){
char ch;
bool f = false;
while((ch = getchar()) < '0' || ch > '9')
if(ch == '-') f = true;
int res = ch - 48;
while((ch = getchar()) >= '0' && ch <='9')
res = res * 10 - ch + 48;
return f ? res + 1 : res;
}
long long jc(long long a){
//求阶乘
if(a == 0) return 1;
long long ans = 1;
for(int i=1;i<=a;i++)
ans *= i;
return ans; //b = !a
}
long long C(long long n,long long m){
return jc(n) / (jc(m) * jc(n - m));
}
//组合数公式:Cn^m = !n / (!m * !(n - m))
long long t,k,n,m;
long long sum,x;
int main(){
t = read(); k = read();
while(t--){
x = 0;
n = read(); m = read();
//sum = jc(n) / (jc(m) * jc(n - m));
for(long long i=1;i<=n;i++){
//int j = min(i , m);
for(long long j=1;j<=min(i,m);j++){
//sum = jc(i) / (jc(j) * jc(i - j));
if(C(i,j) % k == 0)
x++;
}
}
printf("%lld\n",x);
}
return 0;
}
15分,我现在用了组合数的递推公式,按理说应该更快了,但。。(想不通,数据范围在那里啊)
c[i][j]即为从i件物品中选j件的方案数。如果第i件物品不选,方案数就变为c[i-1][j],如果选第i件物品,方案数就变为c[i-1][j-1],总方案数就为两种情况的方案数之和
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2005;
long long read(){
char ch;
bool f = false;
while((ch = getchar()) < '0' || ch > '9')
if(ch == '-') f = true;
int res = ch - 48;
while((ch = getchar()) >= '0' && ch <='9')
res = res * 10 - ch + 48;
return f ? res + 1 : res;
}
long long t,k,n,m;
long long sum,x,C[maxn][maxn];
int main(){
t = read(); k = read();
while(t--){
x = 0;
C[1][0] = C[1][1] = 1;
n = read(); m = read();
for(long long i=2;i<=n;i++){
C[i][0] = 1;
for(long long j=1;j<=min(i,m);j++){
C[i][j] = C[i-1][j] + C[i-1][j-1];
if(C[i][j] % k == 0)
x++;
}
}
printf("%lld\n",x);
}
return 0;
}
为了提高效率,我们可以进行进一步的优化,就是预处理出组合数从而求出所有区间的满足条件的组合数个数,这里就要用到二维前缀和
杨辉三角
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2005;
inline int read() {
int x=0,f=1;
char ch=getchar();
while(ch>'9'||ch<'0'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
long long t,k,n,m;
long long sum[maxn][maxn],x,C[maxn][maxn];
void work(){
for(int i=1;i<=2000;i++){
C[i][0] = 1;
C[i][i] = 1;
}
C[1][1] = 1;
for(long long i=2;i<=2000;i++)
for(long long j=1;j<i;j++){
C[i][j] = (C[i-1][j] + C[i-1][j-1]) % k;
}
for(long long i=1;i<=2000;i++){
for(long long j=1;j<=i;j++){
sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1];
if(C[i][j] == 0)
sum[i][j]++ ;
}
sum[i][i+1] = sum[i][i];
}
}
int main(){
memset(C,0,sizeof(C));
memset(sum,0,sizeof(sum));
t = read(); k = read();
work();
while(t--){
n = read(); m = read();
m = min(n , m);
printf("%lld\n",sum[n][m]);
}
return 0;
}
还有一个事不得不说,我改了一下午竟然发现是自己的快读打错了:
修改后:
暴力 40分
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
inline int read() {
int x=0,f=1;
char ch=getchar();
while(ch>'9'||ch<'0')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
long long jc(long long a){
//求阶乘
if(a == 0) return 1;
long long ans = 1;
for(int i=1;i<=a;i++)
ans *= i;
return ans; //b = !a
}
long long C(long long n,long long m){
return jc(n) / (jc(m) * jc(n - m));
}
//组合数公式:Cn^m = !n / (!m * !(n - m))
long long t,k,n,m;
long long sum,x;
int main(){
t = read(); k = read();
while(t--){
x = 0;
n = read(); m = read();
//sum = jc(n) / (jc(m) * jc(n - m));
for(long long i=1;i<=n;i++){
//int j = min(i , m);
for(long long j=1;j<=min(i,m);j++){
//sum = jc(i) / (jc(j) * jc(i - j));
if(C(i,j) % k == 0)
x++;
}
}
printf("%lld\n",x);
}
return 0;
}
递推公式 70
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2005;
inline int read() {
int x=0,f=1;
char ch=getchar();
while(ch>'9'||ch<'0'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
long long t,k,n,m;
long long sum,x,C[maxn][maxn];
int main(){
t = read(); k = read();
while(t--){
x = 0;
C[1][0] = C[1][1] = 1;
n = read(); m = read();
for(long long i=2;i<=n;i++){
C[i][0] = 1;
for(long long j=1;j<=min(i,m);j++){
C[i][j] = C[i-1][j] + C[i-1][j-1];
if(C[i][j] % k == 0)
x++;
}
}
printf("%lld\n",x);
}
return 0;
}
洛谷P2822组合数问题的更多相关文章
- 洛谷P2822 组合数问题(题解)
https://www.luogu.org/problemnew/show/P2822(题目传送) 先了解一下有关组合数的公式:(m在上,n在下) 组合数通项公式:C(n,m)=n!/[m!(n-m) ...
- 洛谷P2822 组合数问题
输入输出样例 输入样例#1: 1 2 3 3 输出样例#1: 1 输入样例#2: 2 5 4 5 6 7 输出样例#2: 0 7 说明 [样例1说明] 在所有可能的情况中,只有C_2^1 = 2C21 ...
- 洛谷 P2822 组合数问题
题目描述 组合数C_n^mCnm表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三个物品中选择两个物品可以有(1,2),(1,3),(2,3)这三种选择方法.根据组合数的 ...
- 洛谷——P2822 组合数问题
https://www.luogu.org/problem/show?pid=2822 题目描述 组合数C_n^mCnm表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三 ...
- 【洛谷P2822 组合数问题】
题目连接 #include<iostream> #include<cstring> #include<cstdio> #include<cctype> ...
- 洛谷P2822 组合数问题 杨辉三角
没想到这道题竟然这么水- 我们发现m,n都非常小,完全可以O(nm)O(nm)O(nm)预处理出stripe数组,即代表(i,j)(i,j)(i,j) 及其向上的一列的个数,然后进行递推即可. #in ...
- 洛谷 P2822 组合数问题 题解
今天又考试了...... 这是T2. Analysis 考试时想了一个判断质因数个数+打表的神奇方法,但没在每次输入n,m时把ans置0,50分滚粗. 看了题解才发现原来是杨辉三角+二维前缀和,果然还 ...
- 【题解】洛谷P2822 [NOIP2016TG ]组合数问题 (二维前缀和+组合数)
洛谷P2822:https://www.luogu.org/problemnew/show/P2822 思路 由于n和m都多达2000 所以暴力肯定是会WA的 因为整个组合数是不会变的 所以我们想到存 ...
- 【洛谷p2822】组合数问题
(突然想 ??忘掉了wdt) (行吧那就%%%hmr) 组合数问题[传送门] (因为清明要出去培训数学知识所以一直在做数论) 组合数<=>杨辉三角形(从wz那拐来的技能 ...
随机推荐
- 搭建ELK收集Nginx日志
众所周知,ELK是日志收集套装,这里就不多做介绍了. 画了一个粗略的架构图,如下: 这里实际用了三个节点,系统版本为CentOS6.6,ES版本为2.3.5,logstash版本为2.4.0,kiba ...
- centos7添加虚拟IP
1.在网络配置文件中添加虚拟IP,vi /etc/sysconfig/network-scripts/ifcfg-eno16777736 TYPE="Ethernet" BOOTP ...
- PyPt5 浏览器实例
title: PyPt5 浏览器实例 date: 2018-02-02 13:40:03 tags: Python PyQt5 便携浏览器 categries: Python --- 导入包 pyQt ...
- 团体程序设计天梯赛 L3-004. 肿瘤诊断
数组的大小不能开太大,否则会出现段错误 用bfs而不用dfs,dfs存储太多中间过程,会超内存 #include <stdio.h> #include <stdlib.h> # ...
- https 协议信息查看
https://www.ssllabs.com/ssltest/」—————————
- GO_02:GO语言开篇
Go的发展史 Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发,后 ...
- dp px 互转工具类
public class DensityUtils { public static int dpToPx(Context context,int dp){ float density = contex ...
- 为了拿Ph.D而做出的诺贝尔奖
- Centos7 设置静态IP后重启网络服务出错
systemctl restart networkJob for network.service failed because the control process exited with erro ...
- urllib模块和urllib2模块的区别
一开始我以为urllib2模块单纯是urllib模块的升级版,因为我看到它们都有urlopen方法,但是经过查找资料,发现两者差别还是很大的. 这是我在网上看到的总结: urllib2可以接受一个Re ...