zoj 3841 Cards
题意:给你52张牌,已知一个牌的序列,然后利用剩余的牌,能排成多少个序列,这个序列比已知的序列字典序小。
思路:从左到右尽可能放比已知序列相应位置小,找不到就放一样,然后求组合数就可以。多重集排列定理:令s时一个多重集,有k个不同类型的元素,各元素的重数分别为n1,n2......nk,令s的大小为n=n1+n2+......nk,则s的排列数为 n!/(n1!*n2!.....nk!);
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define ll long long
#define maxn 100010
using namespace std;
const int mod=; char str[maxn];
int num[maxn];
ll f[maxn];
map<char,int>q; void inti()
{
f[]=;
for(int i=; i<=; i++)
f[i]=(f[i-]*i)%mod;
q['A']=;
for(int i=; i<=; i++)
q[''+i]=i;
q['']=;
q['J']=;
q['Q']=;
q['K']=;
} ll mult_mod(ll a,ll b,ll c)
{
a%=c;
b%=c;
ll ret=;
ll tmp=a;
while(b)
{
if(b&)
{
ret+=tmp;
if(ret>c) ret-=c;
}
tmp<<=;
if(tmp>c) tmp-=c;
b>>=;
}
return ret;
} ll pow_mod(ll a,ll n)
{
ll ret=;
ll temp=a%mod;
while(n)
{
if(n&)
{
ret=(ret*temp)%mod;
}
temp=(temp*temp)%mod;
n>>=;
}
return ret;
}
int main()
{
inti();
while(scanf("%s",str)!=EOF)
{
int k=strlen(str);
for(int i=; i<=; i++)
{
num[i]=;
}
int cnt=;
for(int i=; i<k; i++)
{
num[q[str[i]]]--;
cnt--;
if(str[i]=='') i++;
}
ll ans=;
int m=cnt;
bool flag=false;
for(int i=; i<k; i++)
{
if(cnt==) break;
for(int j=; j<=; j++)
{
if(num[j]>&&j<q[str[i]])
{
ll x=f[cnt-];
ll y=;
for(int c=; c<=; c++)
{
if(c==j)
{
y=(y*f[num[c]-])%mod;
continue;
}
y=(y*f[num[c]])%mod;
}
x=(x*pow_mod(y,mod-))%mod;
ans=(ans+x)%mod;
}
}
if(num[q[str[i]]]==)
{
flag=true;
break;
}
num[q[str[i]]]--;
cnt--;
if(str[i]=='') i++;
}
if(!flag&&m<-m)
ans=(ans+)%mod;
printf("%lld\n",ans);
}
return ;
}
zoj 3841 Cards的更多相关文章
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- zoj 2734 Exchange Cards【dfs+剪枝】
Exchange Cards Time Limit: 2 Seconds Memory Limit: 65536 KB As a basketball fan, Mike is also f ...
- POJ 1511 Invitation Cards (ZOJ 2008) 使用优先队列的dijkstra
传送门: http://poj.org/problem?id=1511 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1008 ...
- 【ZOJ】3380 Patchouli's Spell Cards
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3957 题意:m个位置,每个位置填1~n的数,求至少有L个位置的数一样的概率(1 ...
- zoj 3380 Patchouli's Spell Cards 概率DP
题意:1-n个位置中,每个位置填一个数,问至少有l个数是相同的概率. 可以转化求最多有l-1个数是相同的. dp[i][j]表示前i个位置填充j个位置的方案数,并且要满足上面的条件. 则: dp[i] ...
- ZOJ 2852 Deck of Cards DP
题意: 一一个21点游戏. 1. 有三个牌堆,分别为1X,2X,3X. 2. 纸牌A的值为1,纸牌2-9的值与牌面面相同,10(T).J.Q.K的值为10,而而joke(F)的值为 任意大大. 3. ...
- Deck of Cards ZOJ - 2852 dp 多决策 三维 滚动更新
题意:一个特殊21点游戏 具体http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2852 题解:建一个三维dp,表示三个卡槽分别 ...
- ZOJ 3380 Patchouli's Spell Cards
方案数,$dp$. 总的方案数有$n^m$种,符合要求的直接算不好算,可以算反面,即不符合要求的. 设$dp[i][j]$表示前$i$种等级填了$j$个位置,那么$dp[i][j]=sum(dp[i- ...
- 重拾ZOJ 一周解题
ZOJ 2734 Exchange Cards 题目大意: 给定一个值N,以及一堆卡片,每种卡片有一个值value和数量number.求使用任意张卡片组成N的方式. 例如N = 10 ,cards(1 ...
随机推荐
- 手动创建Servlet--J2EE学习笔记
Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层. 使用 Serv ...
- Java基础知识强化之IO流笔记33:转换流之InputStreamReader的使用
1. InputStreamReader的使用 InputStreamReader(InputStream is):用默认的编码读取数据 InputStreamReader(InputStream i ...
- JPA entity versioning (@Version and Optimistic Locking)
详情见: http://www.byteslounge.com/tutorials/jpa-entity-versioning-version-and-optimistic-locking
- Js 直接下载保存文件
//直接下载保存文件 function Download(filePath) { // 如果中间IFRAME不存在,则添加 if (!document.getElementById("_SA ...
- 整理收藏一份PHP高级工程师的笔试…
注:本文转自 http://www.icultivator.com/p/5535.html 注:本文转自 http://www.yiichina.com/tutorial/57 整理了一份PHP高级工 ...
- c#中var关键字用法
Technorati 标签: C# 转载自csdn:http://blog.csdn.net/robingaoxb/article/details/6175533 var关键字是C# 3.0开始新 ...
- C#当中的多线程_线程基础
前言 最近工作不是很忙,想把买了很久了的<C#多线程编程实战>看完,所以索性把每一章的重点记录一下,方便以后回忆. 第1章 线程基础 1.创建一个线程 using System; usin ...
- Shell: extract more from listener.log(分析监听日志)
最近遇到了两起数据库连接数不足的问题, 通常都会预留一些会话增加的情况, 但在一些特殊情况下如连接风暴(logon storm), 如果在监听中没有做rate限流,对数据库来说巨大的冲击可能会导致数据 ...
- Artificial Intelligence
//**************************************BEST-FS ALRORITHM IN ARTIFICAL INTELLIGENCE***************** ...
- 读书笔记之 - javascript 设计模式 - 观察者模式
在事件驱动的环境中,比如浏览器这种持续寻求用户关注的环境中,观察者模式是一种管理人与其任务(确切的讲,是对象及其行为和状态之间的关系)之间的关系的得力工具.用javascript的话来讲,这种模式的实 ...