【组合数学】 ZOJ 3841 Cards

从左到右,能放比当前序列小的就放比当前序列小的,否则就放和当前序列一样的,然后统计组合数。。。

#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 50005
#define maxm 4005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
//#define lson o<<1, L, mid 
//#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

LL f[55];
LL g[55];
int hash[200];
int c[55];
char s[100];

void init()
{
	hash['A'] = 1;
	hash['2'] = 2;
	hash['3'] = 3;
	hash['4'] = 4;
	hash['5'] = 5;
	hash['6'] = 6;
	hash['7'] = 7;
	hash['8'] = 8;
	hash['9'] = 9;
	hash['J'] = 11;
	hash['Q'] = 12;
	hash['K'] = 13;
	f[0] = 1;
	for(int i = 1; i <= 50; i++) f[i] = f[i-1] * i % mod;
	g[50] = powmod(f[50], mod-2);
	for(int i = 49; i >= 0; i--) g[i] = g[i+1] * (i + 1) % mod;
//	for(int i = 1; i <= 4; i++) printf("PPPP %lld\n", f[i]);
}

void work()
{
	memset(c, 0, sizeof c);
	int len = strlen(s), cnt = 0;
	for(int i = 0; i < len; i++) {
		if(s[i] == '1') c[10]++, i++;
		else c[hash[s[i]]]++;
		cnt++;
	}
	int dep = 52 - cnt;
	if(dep == 0 || dep == 52) {
		printf("0\n");
		return;
	}
	for(int i = 1; i <= 13; i++) c[i] = 4 - c[i];
	LL ans = 0;
	for(int k = 0; k < len; k++) {
		int t;
		if(s[k] == '1') t = 10, k++;
		else t = hash[s[k]];
		if(dep == 0) break;
		for(int i = 1; i < t; i++) {
			if(c[i]) {
				c[i]--;
				LL tt = 1;
				int tot = dep-1;
				for(int j = 1; j <= 13; j++)
					if(c[j]) {
						tt = tt * f[tot] % mod * g[c[j]] % mod * g[tot - c[j]] % mod;
						tot -= c[j];
					}
				ans = (ans + tt) % mod;
				c[i]++;
			}
		}
		if(c[t]) c[t]--;
		else break;
		dep--, cnt--;
	}
	if(dep == 0 && cnt) ans++;
	printf("%lld\n", ans % mod);
}

int main()
{
	init();
	while(scanf("%s", s)!=EOF) {
		work();
	}
	
	
	return 0;
}


你可能感兴趣的:(ZOJ)