208. Toral Tickets

time limit per test: 0.25 sec.
memory limit per test: 65536 KB
input: standard
output: standard

On the planet Eisiem passenger tickets for the new mean of transportation are planned to have the form of tores.

Each tore is made of a single rectangular black rubber sheet containing N × M squares. Several squares are marked with white, thus encoding the ticket's source and destination.

When the passenger buys the ticket, the ticket booking machine takes the rubber sheet, marks some squares to identify the route of the passenger, and then provides it to the passenger. The passenger next must glue the ticket.

The ticket must be clued the following way. First two its sides of greater length are glued together, forming a cylinder. Next cylinder base circles, each of which has the length equal to the length of the short side of the original rubber sheet, are glued together. They must be glued in such a way, that the cells, sides of which are glued, first belonged to the same row of the sheet. Note that the inner and the outer part of the sheet can be distinguished.

The resulting tore is the valid ticket.

Note that if the original sheet is square, there are two topologically different ways to make a tore out of a rubber sheet.

Ticket material is so perfect and gluing quality is so fine, that no one is able to find the seam, and this leads to some problems. First, the same tore can be obtained using different sheets. More of that, the same sheet can lead to tores that look a bit different.

Now the transport companies of Eisiem wonder, how many different routes they can organize, so that the following conditions are satisfied:

tickets for different routes are represented by different tores;

if some rubber sheet was marked to make the tore for some route, it cannot be used to make the tore for another route.

Help them to calculate the number of routes they can organize.

Input

The first line of the input file contains N and M (1 ≤ N, M ≤ 20).

Output

Output the number of routes Eisiem transport companies can organize.

Sample test(s)

Input

Test #1

2 2

Test #2

2 3

Output

Test #1

6

Test #2

13

题意

给你N和M,对于一个N*M的单面方格纸你可以对它的每个个格子黑白染色,然后把方格纸的长边卷起来,卷成一个圆柱体,然后再把两个短边形成的圆也接起来,形成一个游泳圈的形状(我们染的色只在游泳圈的外表面)。如果对于两种黑白染色方案,通过卷成这样的游泳圈后,是一样的,则这两种方案也是一样的。给定N,M<=20 ,求染色方案总数.


很明显的polya计数,不会的话下面列出了一些参考:

  1. 刘老师的黑书
  2. http://wenku.baidu.com/view/bf92a95f804d2b160b4ec0be.html
  3. http://zhangchi.weebly.com/uploads/8/7/5/5/8755757/polya.pdf

需要高精度。

#include <bits/stdc++.h>
#define rep(_i, _j) for(int _i = 1; _i <= _j; ++_i)
const int inf = 0x3f3f3f3f;
typedef long long LL;
typedef double DB;
using namespace std; const int maxm = 20 + 2;
const int maxn = 400 + 20;
const int maxlen = 140 + 10;
struct big_num {
int d[maxlen], len;
big_num() {
memset(d, 0, sizeof d);
len = 1;
}
inline int & operator [] (int index) {
return d[index];
}
friend big_num operator + (big_num lhs, big_num rhs) {
big_num ret;
ret.len = max(lhs.len, rhs.len);
for(int i = 1; i <= ret.len; ++i) {
ret[i] += lhs[i] + rhs[i];
if(ret[i] > 9) {
ret[i] %= 10, ret[i + 1] += 1;
}
}
if(ret[ret.len + 1] > 0) ++ret.len;
return ret;
}
friend big_num operator / (big_num lhs, int rhs) {
big_num ret;
int remain = 0;
for(int i = lhs.len; 0 < i; --i) {
remain = remain * 10 + lhs[i];
if(remain >= rhs) {
ret[i] = remain / rhs;
remain %= rhs;
}
}
for(ret.len = maxlen - 1; 1 < ret.len; --ret.len) {
if(ret[ret.len] > 0) break;
}
return ret;
}
void print() {
for(int i = len; 0 < i; --i)
printf("%d", d[i]);
}
};
big_num pow2[maxn], ans; int n, m;
int cache[maxn], hash[maxn];
int Ranma[maxm][maxm]; void right_shift() {
for(register int i = 0, t; i < n; ++i) {
t = Ranma[i][m - 1];
for(int j = m - 1; 0 < j; --j) {
Ranma[i][j] = Ranma[i][j - 1];
}
Ranma[i][0] = t;
}
}
void down_shift() {
for(register int i = 0, t; i < m; ++i) {
t = Ranma[n - 1][i];
for(int j = n - 1; 0 < j; --j) {
Ranma[j][i] = Ranma[j - 1][i];
}
Ranma[0][i] = t;
}
}
void rot() {
int tmp[maxm][maxm];
memcpy(tmp, Ranma, sizeof Ranma);
for(int i = 0; i < m; ++i) {
for(int j = n - 1; -1 < j; --j) {
Ranma[i][n - 1 - j] = tmp[j][i];
}
}
swap(n, m);
} int calc() {
int ret = 0;
memset(hash, 0, sizeof hash);
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
cache[i * m + j] = Ranma[i][j];
}
}
for(register int i = 0, uper = n * m, now; i < uper; ++i) {
if(!hash[i]) {
++ret;
for(now = i; !hash[now]; hash[now] = 1, now = cache[now]);
}
}
return ret;
}
void print() {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
printf("%d ", Ranma[i][j]);
}
puts("");
}
puts("");
}
int main() {
#ifndef ONLINE_JUDGE
freopen("208.in", "r", stdin); freopen("208.out", "w", stdout);
#endif pow2[0].d[1] = 1;
for(int i = 1; i < maxn; ++i)
pow2[i] = pow2[i - 1] + pow2[i - 1];
scanf("%d%d", &n, &m);
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
Ranma[i][j] = i * m + j;
}
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
ans = ans + pow2[calc()];
rot();
if(n == m) ans = ans + pow2[calc()];
rot();
ans = ans + pow2[calc()];
rot();
if(n == m) ans = ans + pow2[calc()];
rot();
right_shift();
}
down_shift();
}
ans = ans / (n * m * 2 * (n == m ? 2 : 1));
ans.print();
puts(""); return 0;
}

SGU 208. Toral Tickets的更多相关文章

  1. 【SGU 390】Tickets (数位DP)

    Tickets   Description Conductor is quite a boring profession, as all you have to do is just to sell ...

  2. sgu208:Toral Tickets(P&#243;lya定理)

    题意简述:给你N和M,对于一个N∗M的单面方格纸你能够对它的每 个个格子黑白染色.然后把方格纸的长边卷起来,卷成一个圆柱体,然后再把 两个短边形成的圆也接起来.形成一个游泳圈的形状(我们染的色仅仅在游 ...

  3. POJ2828 Buy Tickets[树状数组第k小值 倒序]

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19012   Accepted: 9442 Desc ...

  4. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  5. Tickets——H

    H. Tickets Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this i ...

  6. POJ 2828 Buy Tickets(线段树 树状数组/单点更新)

    题目链接: 传送门 Buy Tickets Time Limit: 4000MS     Memory Limit: 65536K Description Railway tickets were d ...

  7. SGU 495. Kids and Prizes

    水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...

  8. ACM: SGU 101 Domino- 欧拉回路-并查集

    sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Desc ...

  9. 【SGU】495. Kids and Prizes

    http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...

随机推荐

  1. Linux用户创建及权限管理

    作业一: 1,新建用户natasha,uid为1000,gid为555,备注信息为“master” useradd natasha            vim /etc/passwd         ...

  2. mysql四-2:多表查询

    一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 #建表 create table department( id int, name varchar(20) ); create ta ...

  3. Mybatis中jdbcType和javaType对应关系

    Mybatis中javaType和jdbcType对应关系 JDBC Type           Java Type CHAR                String VARCHAR       ...

  4. 手脱UPX v0.89.6 - v1.02

    声明: 只为纪录自己的脱壳历程,高手勿喷 这个壳的脱法很多一般都一步直达的,步过我喜欢ESP定律 1.载入OD,在入口下一行ESP定律运行一次 > pushad ; //入口 BE mov es ...

  5. 单例 ------ C++实现

    基础知识掌握: 单例考虑三点:内存何时释放.运行速度如何.多线程下能否保证只有一个实例 如果获取对象的返回值类型是引用,返回值赋值给变量而不是引用会进行对象的拷贝,这样就会出现两个对象,可以把显示声明 ...

  6. php网摘收藏

    1.thinkphp3.2.3开发手册: http://document.thinkphp.cn/manual_3_2.html 2.ThinkPHP3.2.3的函数汇总:http://www.thi ...

  7. Java基础-synchronized关键字的用法(转载)

    synchronized--同步 顾名思义是用于同步互斥的作用的. 这里精简的记一下它的使用方法以及意义: 当synchronized修饰 this或者非静态方法或者是一个实例的时候,所同步的锁是加在 ...

  8. uboot&kernel&system

  9. 重构改善既有代码设计--重构手法05:Introduce Explaining Variable (引入解释性变量)

      发现:你有一个复杂的表达式. 解决:将该复杂的表达式(或其中的部分)的结果放进一个临时变量,并以此变量名称来解释表达式用途. //重构前 if((platform.toUpperCase().in ...

  10. opencv 高级拼接函数Stitcher

    Stitcher https://docs.opencv.org/trunk/d8/d19/tutorial_stitcher.html http://blog.csdn.net/czl389/art ...