当前位置:   article > 正文

zoj 3841 Cards(组合数学)_zoj3841

zoj3841

题目链接

Cards

Time Limit: 2 Seconds      Memory Limit: 65536 KB

EdwardGy has a poker (52 cards excluding jokers). One day, he saw some cards was lined up on the table. He wanted to know how many ways he can place all of the rest cards in a row with a lower lexicographic order than the line of the cards which were already on the table.

The lexicographic order of the cards is A < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < J < Q < K, and the colors should be ignored. If the cards already on the table is

AA22334455667788991010JJKKK

EdwardGy have only five ways:

AA22334455667788991010JJQQQQK
AA22334455667788991010JJQQQKQ
AA22334455667788991010JJQQKQQ
AA22334455667788991010JJQKQQQ
AA22334455667788991010JJKQQQQ

Input

There are multiple test cases. Each test case has only one line, a valid string indicating the cards on the table.

Output

For each test cases, output the remainder of the number of ways modulo by 109+7.

Sample Input
AA22334455667788991010JJKKK
Sample output
5

题意:有52张扑克牌(除开了大王、小王)。现在桌子上摆了一些牌,问用剩下的所有牌摆放,有多少种摆法的字典序比桌子上的低?两种摆法不同的定义为:至少有一个位置的牌不同。

题解:设剩余的牌的个数为x,剩余的每种牌的个数为a1,a2,....a13。枚举求第i个位置比桌子上的小,摆的是j,前i-1个位置与桌子上的相同的摆法数,为(n-i)!/(a1!* a2!*.....(aj-1)!*..a13!)。注意特判剩余的牌的数目小于桌上的牌数的情况。

代码如下:

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<string.h>
  5. #include<string>
  6. #include<queue>
  7. #include<stack>
  8. #include<map>
  9. #include<set>
  10. #include<stdlib.h>
  11. #include<vector>
  12. #define inff 0x3fffffff
  13. #define nn 110000
  14. #define mod 1000000007
  15. typedef long long LL;
  16. const LL inf64=inff*(LL)inff;
  17. using namespace std;
  18. char s[110];
  19. LL jx[110];
  20. map<char,int>ma;
  21. void init()
  22. {
  23. jx[0]=1;
  24. int i;
  25. for(i=1;i<=55;i++)
  26. {
  27. jx[i]=(jx[i-1]*i)%mod;
  28. }
  29. ma['A']=1;
  30. for(i=2;i<=9;i++)
  31. ma[i+'0']=i;
  32. ma['1']=10;
  33. ma['J']=11;
  34. ma['Q']=12;
  35. ma['K']=13;
  36. }
  37. int num[15];
  38. LL po(LL x,int y)
  39. {
  40. LL re=1;
  41. LL tem=x;
  42. while(y)
  43. {
  44. if(y&1)
  45. {
  46. re=(re*tem)%mod;
  47. }
  48. tem=(tem*tem)%mod;
  49. y>>=1;
  50. }
  51. return re;
  52. }
  53. int main()
  54. {
  55. int i,j;
  56. init();
  57. while(scanf("%s",s)!=EOF)
  58. {
  59. int ls=strlen(s);
  60. int gs=52;
  61. for(i=1;i<=13;i++)
  62. num[i]=4;
  63. for(i=0;i<ls;i++)
  64. {
  65. num[ma[s[i]]]--;
  66. gs--;
  67. if(s[i]=='1')
  68. i++;
  69. }
  70. LL ans=0;
  71. LL tem;
  72. LL fc;
  73. int fuck=gs;
  74. bool ok=true;
  75. for(i=0;i<ls;i++)
  76. {
  77. if(gs==0)
  78. break;
  79. for(j=1;j<=13;j++)
  80. {
  81. if(num[j]>0&&j<ma[s[i]])
  82. {
  83. tem=jx[gs-1];
  84. fc=jx[num[j]-1];
  85. for(int k=1;k<=13;k++)
  86. {
  87. if(k==j)
  88. continue;
  89. fc=(fc*jx[num[k]])%mod;
  90. }
  91. tem=(tem*po(fc,mod-2))%mod;
  92. ans=(ans+tem)%mod;
  93. }
  94. }
  95. if(num[ma[s[i]]]==0)
  96. {
  97. ok=false;
  98. break;
  99. }
  100. num[ma[s[i]]]--;
  101. gs--;
  102. if(s[i]=='1')
  103. i++;
  104. }
  105. if(ok&&fuck<52-fuck)
  106. ans=(ans+1)%mod;
  107. printf("%lld\n",ans);
  108. }
  109. return 0;
  110. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/75463
推荐阅读
相关标签
  

闽ICP备14008679号