AOJ2025 Eight Princes

我们查看更一般的情况,设人数为m

则n < m * 2无解

然后n为奇数的情况:

我们把一个人和一个空格打包,于是剩下m个"人"和n - m个空格,随便排列这些"人",然后把空格插入

本质不同的"人"的圆周排列有(m - 1)!个,对于每个排列,有m个位置插入n - m个空格,有C(n - m - 1, m - 1)中方法,然后对于一个排列,圆周排列有n个

ans = C(n - m - 1, m - 1) * (m - 1)! * n

最后是n为奇数的情况:

我们把相对的两个点打包,然后就变成一个长度为n / 2的圈

等价于n / 2中选m个,一个有k段连续的1的方案对应原来的2k种方案

于是ans = n / 2 * (m - 1)! * Σ (2k * C(n / 2 - m - 1, k - 1) * C(m, k)) (其中1 ≤ k ≤ m)

然后题目比较鬼畜。。。我还是上py好了。。。

 1 def C(n, m) :
 2     if (n < m) : return 0
 3     return fac[n] / fac[m] / fac[n - m]
 4
 5 def work_odd(n, m) :
 6     return n * C(n - m - 1, m - 1) * fac[m - 1]
 7
 8 def work_even(n, m) :
 9     res = 0
10     n /= 2
11     i = m
12     while (i > 0) :
13         res = (res + C(n - m - 1, i - 1) * C(m, i)) * 2
14         i -= 1
15     return res * fac[m - 1] * n;
16
17 fac = [1] * 105
18 for i in range(1, 100) : fac[i] = fac[i - 1] * i
19 n = input()
20 m = 8
21 while (n > 0) :
22     if (n < m * 2) : print 0
23     elif (n % 2 == 1) : print work_odd(n, m)
24     else : print work_even(n, m)
25     n = input();

时间: 2024-10-26 05:28:22

AOJ2025 Eight Princes的相关文章

B. Princesses and Princes

B. Princesses and Princes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The King of Berland Polycarp LXXXIV has nn daughters. To establish his power to the neighbouring kingdoms he wants

9.Solr4.10.3数据导入(post.jar方式和curl方式)

转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.使用post.jar方式 java -Durl=http://192.168.137.168:8080/solr/mycore/update -Ddata=files -jar /usr/local/solr-4.10.3/example/exampledocs/post.jar /usr/local/solr-4.10.3/example/multicore/exampledocs/ipod_othe

HDU 4685 Prince and Princess

Prince and Princess Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 468564-bit integer IO format: %I64d      Java class name: Main There are n princes and m princesses. Princess can marry any prince. But prin

2-8 无人值守安装服务器搭建 日志

无人值守安装服务器搭建PXE + DHCP+TFTP+ Kickstart+ FTPdhcp PXE tftp vsftpd system-config-kickstart ###################################################################################################################################################################

A Game of Thrones(9) - Tyrion

Somewhere in the great stone maze of Winterfell, a wolf howled. The sound hung over the castle like a flag of mourning. Tyrion Lannister looked up from his books and shivered, though the library was snug and warm. Something about the howling of a wol

HDU 4685 Prince and Princess(二分图 + 强连通)

Problem Description There are n princes and m princesses. Princess can marry any prince. But prince can only marry the princess they DO love. For all princes,give all the princesses that they love. So, there is a maximum number of pairs of prince and

新概念英语第三册1-20课(转)

Lesson 1  A Puma at large 逃遁的美洲狮 1.美洲狮是大的,Pumas are large,猫一样的 cat-like动物 animals它们被发现 which are found在美洲. in America. 美洲狮是一种大型猫科动物Pumas are large, cat-like animals它们被发现在美洲. which are found in America. 美洲狮一种生活在美洲的大型猫科动物, 2.当时-报告When reports提交到伦敦动物园 c

英雄无敌3地图的中英文对照

一直都很喜欢英雄无敌3,而且我觉得这个翻译真的很有水平,留一份存档在这里欣赏(红字表示翻译的特别好): A Viking We Shall Go 维京风暴! A Warm and Familiar Place 竞争上岗 Adventures of Jared Haret 胜利逃亡 All for One 臣服 And One for All 一统江山 Arrogance 傲气冲天 Ascension 即位 Back For Revenge 卷土重来 Barbarian Breakout 野蛮人的

A Game of Thrones(3) - Daenerys

Her brother held the gown up for her inspection. “This is beauty. Touch it. Go on. Caress(爱抚,抚抱) the fabric.” Dany touched it. The cloth was so smooth that it seemed to run through her fingers like water. She could not remember ever wearing anything