uva 10294 Arif in Dhaka (First Love Part 2)

来源:互联网 发布:淘宝秒杀信息收集 编辑:程序博客网 时间:2024/06/11 19:56

Problem L

Arif in Dhaka (First Love Part 2)

Input: standard input

Output: standard output

Time Limit: 2 seconds

 

Our hero Arif is now in Dhaka (Look at problem 10244 – First Love if you want to know more about Arif, but that information is not necessary for this problem. In short, Arif is a brilliant programmer working at IBM) and he is looking for his first love. Days pass by but his destiny theory is not working anymore, which means that he is yet to meet his first love. He then decides to roam around Dhaka on a rickshaw (A slow vehicle pulled by human power), running DFS (by physical movement) and BFS (with his eyes) on every corner of the street and market places to increase his probability of reaching his goal. While roaming around Dhaka he discovers an interesting necklace shop. There he finds some interesting necklace/bracelet construction sets. He decides to buy some of them, but his programmer mind starts looking for other problems. He wants to find out how many different necklace/bracelet can be made with a certain construction set. You are requested to help him again. The following things are true for a necklace/bracelet construction set.

 

a)      All necklace/bracelet construction sets has a frame, which has N slots to place N beads.

b)      All the slots must be filled to make a necklace/bracelet.

c)      There are t types of beads in a set. N beads of each type are there in the box. So the total number of beads is tN (t multiplied by N), of which exactly N can be used at a time.

 


Fig: Different types of necklace for t=2 and different value of N

 

The figure above shows necklaces for some different values of N (Here, t is always 2). Now let’s turn out attentions to bracelets. A bracelet is a necklace that can be turned over (A junior programmer in Bangladesh says that wrist watch is a necklace (Boys!!! Don’t mind :-))). So for abracelet the following two arrangements are equivalent. Similarly, all other opposite orientation or mirror images are equivalent.

 

 

So, given the description of a necklace/bracelet construction set you will have to determine how many different necklace and bracelet can be formed with made with that set 

 

Input

The input file contains several lines of input. Each line contains  two positive integers N(0<N<51) and t(0<t<11) as described in the problem statement. Also note that within this input range inputs will be such that no final result will exceed 11 digits. Input is terminated by end of file.  

 

Output

For each line of input produce one line of output which contains two round numbers NN and NB separated by a single space, where NN is the number of total possible necklaces and NB is the number of total possible bracelets for the corresponding input set. 

 

Sample Input

5 2

5 3

5 4

5 5

 

Sample Output

8 8

51 39

208 136

629 377


(Math Lovers’ Contest, Problem Setter: Shahriar Manzoor)


题目大意:一条含有N个珠子的项链,每个珠子都要t种不同的颜色。旋转后相同的项链为同一种。旋转、翻转后相同的手链为同一种。问有多少种本质不同的项链,手链。N < 51, t < 11.


这是一道Polya定理的直接应用题。

在理解了Polya定理的基础上,这道题几乎没有思维难度。


Polya定理:设G是集合X上的一个置换群,X中每一个元素都可以被染成k种颜色,则不等价的着色数为:P = 1 / |G| * sigma ( k ^ nc ( g ) ) 其中 nc(g)为置换g中循环节的个数

|G|  是置换群中置换的个数

求和部分 是对置换g保持不变的着色集合。

对每一个元素的置换 可以诱导出对着色方案的置换


用Polya定理解题,首先应建立作用在n个对象上的置换群。在简历置换群的过程中,若对允许的诸如旋转、翻转等等变换找完,则相应的置换构成的集合G一定对置换的乘法封闭。


算法分析:

(1) 旋转:对于某个旋转,第0个元素对应着顺时针的第s个元素(旋转了s下)则该置换的循环节数为gcd(s,n)

假设一下转s,转k下回到原位则 k = lcm(s, n) / s = s * n / gcd ( s, n) / s = n / gcd ( s , n ) 这个循环节包含了k个元素

每个循环节都包含了k个元素 一共有 n / k = gcd ( s , n ) 个循环节

(2)翻转:当n是奇数时,正n边形的对称轴有n条,每条对称轴形成(n-1)/2个长度为2的循环与1个长度为1的循环

当n是偶数是,与正n边形的交点都是顶点的对称轴有n/2条,每条对称轴形成(n-2)/2个长度为2的循环,与2个长度为1的循环。与正n边形的交点都不是顶点对称轴有n/2条,每条对称轴形成n/2个长度为2的循环。

(画一个顶点数多一点的正多边形也就看出来了)


AC代码

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#include <cstdlib>#include <cassert>using namespace std;typedef long long LL;LL gcd ( LL a , LL b ) { if ( b == 0 ) {return a ;}return gcd ( b , a % b ) ;}LL p ( LL x , LL n ){LL r = 1LL;for ( int i = 0 ; i < n ; i++ ) {r *= x ;}return r;}int main(){#ifdef DEBUGFILE *fPtr = freopen("a.in", "r", stdin);assert(fPtr != NULL);fPtr = freopen("a.out", "w", stdout);assert(fPtr != NULL);#endif//read n kLL n , k ;while ( cin >> n >> k){//calc |G|LL tot = 0LL;LL r = 0;//loop step 0 - n - 1 add tot calc gcd tot += n;for ( LL i = 0LL ; i < n ; i++ ) {LL d = gcd ( n , i ) ;r += p ( k , d ) ;//cout << r << " " ;}//print nnassert ( r > 0 );cout << r / tot << " " ;//odd (n - 1 ) / 2 + 1 node n symif ( n % 2 == 1) {tot += n;r += n * p ( k , ( n - 1 ) / 2 + 1 ) ;}//even  n/2 axis n / 2 node ---- n / 2 axis (n - 2) / 2 + 2else {tot += n;r += n / 2 * p ( k , n / 2) ;r += n / 2 * p ( k , ( n - 2 ) / 2 + 2 ) ;}//print BBassert ( r > 0 );cout << r / tot << endl;}return 0;}


加了很多空格,果然清新了很多。

注释程序代码流程,然后编写,正确率高了很多,一把过样例。


原创粉丝点击