我看大多数人的博客只说了一句:找规律得答案为(n + m) / gcd(n, m)

不过神题的题解还须神人写。。

We can associate at each cell a base 3-number, the log3(R) most significant digits is the index of the row of the cell and the log3(C) least significant digits is the index of his column.

What are the transformation now ?
position in row-major order is rC+c
position in column-major order is cR+r

We should shift down by log3(C) the most significant digits and shift up the least significant digits by log3(R).
C=3^6, R=3^4

now : rrrrcccccc (rrrr)(cccccc)
then: ccccccrrrr (cccc)(ccrrrr)

the first 4 digit are always the number of row (0-indexed) and the last 6 digit the number of column of the cell (0-indexed)
Now this process is valid for each possible r or c, so we can choose r=1 and c=0 and find a the length of this recurring cycle.
Calling L the length of this basic cycle, all other cycle are combination of this one so the only possible length are divisor of L, so the solution of our problem is (m+n)/L
rrrr=0001
cccccc=000000
day 0 : 0001000000 (0001)(000000)
day 1 : 0000000001 (0000)(000001)
day 2 : 0000010000 (0000)(010000)
day 3 : 0100000000 (0100)(000000)
day 4 : 0000000100 (0000)(000100)
day 5 : 0001000000 (0001)(000000)
For solving this problem we can find the the minimal x such that x*n mod (n+m)=0, this imply x=gcd(n, n+m)=gcd(n, m).
The solution of our original problem is (n+m)/x or (n+m)/gcd(n,m).

从0开始逐行给格子进行编号,然后每个格子用一个三进制数表示。还是以34×36的矩形为例,这样每个格子都可以用一个10位三进制数(R1R2R3R4)(C1C2C3C4C5C6)表示,而且高四位是行标,低六位是列标(行和列都是从0开始的)。

比如第1行第0列的格子的标号为(0001)(000000),在执行操作的时候,第一行的数会填满第0~8列,所以这个数就变到了第0行第9列,表示成三进制数就是(0000)(000100)。

更一般地位于(R1R2R3R4)(C1C2C3C4C5C6)的格子的数会变到(C3C4C5C6)(R1R2R3R4C1C2)处。

也就是每次变换每个位置上的数字会右移4位。所以要使所有的数字都回到原位移动的最少次数就是(n + m) / gcd(n, m)

 #include <cstdio>
typedef long long LL; LL gcd(LL a, LL b) { return b == ? a : gcd(b, a%b); } int main()
{
int T;
long long m, n;
scanf("%d", &T);
for(int kase = ; kase <= T; kase++)
{
scanf("%lld%lld", &m, &n);
printf("Case %d: %lld\n", kase, (m + n)/gcd(m, n));
} return ;
}

代码君

UVa 11774 (置换 找规律) Doom's Day的更多相关文章

  1. 紫书 习题8-5 UVa 177 (找规律)

    参考了https://blog.csdn.net/weizhuwyzc000/article/details/47038989 我一开始看了很久, 拿纸折了很久, 还是折不出题目那样..一脸懵逼 后来 ...

  2. 紫书 习题 8-20 UVa 1620 (找规律+求逆序对)

    这道题看了半天没看出什么规律, 然后看到别人的博客, 结论是当n为奇数且逆序数为奇数的时候 无解, 否则有解.但是没有给出证明, 在网上也找到详细的证明--我也不知道是为什么-- 求逆序对有两种方法, ...

  3. 紫书 例题8-12 UVa 12627 (找规律 + 递归)

    紫书上有很明显的笔误, 公式写错了.g(k, i)的那个公式应该加上c(k-1)而不是c(k).如果加上c(k-1)那就是这一次 所有的红气球的数目, 肯定大于最下面i行的红气球数 我用的是f的公式, ...

  4. 紫书 例题 10-25 UVa 1363(找规律)

    可以发现余数是成一段一段的等差数列的. 在商数同的时候,余数是成首项为第一个数的余数,公差 为商数的等差数列. 利用这个性质求解即可. #include<cstdio> #include& ...

  5. UVA 11774 - Doom&#39;s Day(规律)

    UVA 11774 - Doom's Day 题目链接 题意:给定一个3^n*3^m的矩阵,要求每次按行优先取出,按列优先放回,问几次能回复原状 思路:没想到怎么推理,找规律答案是(n + m) / ...

  6. 【数论,找规律】Uva 11526 - H(n)

    原来做过的题再看还是没想出来,看来当时必然没有真正理解.这次回顾感觉理解更透彻了. 网上的题解差不多都是一个版本,而且感觉有点扯.根据n=20猜出来的? 好吧哪能根据一个就猜到那么变态的公式.其实这题 ...

  7. GCD XOR UVA 12716 找规律 给定一个n,找多少对(a,b)满足1<=b<=a<=n,gcd(a,b)=a^b;

    /** 题目:GCD XOR UVA 12716 链接:https://vjudge.net/problem/UVA-12716 题意:给定一个n,找多少对(a,b)满足1<=b<=a&l ...

  8. 递推+高精度+找规律 UVA 10254 The Priest Mathematician

    题目传送门 /* 题意:汉诺塔问题变形,多了第四个盘子可以放前k个塔,然后n-k个是经典的汉诺塔问题,问最少操作次数 递推+高精度+找规律:f[k]表示前k放在第四个盘子,g[n-k]表示经典三个盘子 ...

  9. UVA 10254 - The Priest Mathematician (dp | 汉诺塔 | 找规律 | 大数)

    本文出自   http://blog.csdn.net/shuangde800 题目点击打开链接 题意: 汉诺塔游戏请看 百度百科 正常的汉诺塔游戏是只有3个柱子,并且如果有n个圆盘,至少需要2^n- ...

随机推荐

  1. 巨大bug

    //数据结构关于课程设计--------图书馆管理系统的设计 #include <stdio.h> #include <stdlib.h> #include <strin ...

  2. c# 取 list前100条数据

    [问] List<KeyWord> sortedList = (from a in keyWordList orderby a.Total descending select a).ToL ...

  3. c++ 哪些自定义的数据类型

    http://www.cnblogs.com/ShaneZhang/archive/2013/06/21/3147648.html 这些数据类型是 C99 中定义的,具体定义在:/usr/includ ...

  4. Maven--(一个坑)在settings.xml文件中添加mirrors导致无法新建Maven项目

    这是用新电脑第一次创建Maven项目--当然是一个测试项目.已经差不多忘了该怎样做,所以参考我的博客:http://www.cnblogs.com/wql025/p/4996486.html,这应该是 ...

  5. jquery each函数对应的continue 和 break方法

    continue: return true; break: return false; $("#oGrid").each(function (i, v) { if (i == 0) ...

  6. fork 函数 和vfork 函数的区别

    问题描述:         fork 函数 和vfork 函数的区别 问题解决: fork函数使用: 注:         以上printf 属于标准IO库带缓冲,如果标准输出链接到终端设备,则它是行 ...

  7. 01-04-03【Nhibernate (版本3.3.1.4000) 出入江湖】Criteria API关联查询

    Criteria API关联查询 如果说HQL查询还有需要了解点SQL语法知识,并不是完全彻底面向对象查询, 那么Criterial API就是完全面向对象的查询方式. public IList< ...

  8. 一个IT人士的个人经历,给迷失方向的朋友

    这些日子我一直在写一个实时操作系统内核,已有小成了,等写完我会全部公开,希望能够为国内IT的发展尽自己一份微薄的力量.最近看到很多学生朋友和我当年一样没有方向 ,所以把我的经历写出来与大家共勉,希望能 ...

  9. Android ListView点击失效

    item中存在 ImageButton 等可以点击的组件,这会抢先获得ListView的焦点. 从而导致item点击失效

  10. Spring 与 Hibernate 集成 Transactional设置为只读

    @Transactional标签用于标记ServiceImpl使用事务,并且能够打开一个sessionFactory的session,并且打开事务. 如果在这个标签为@Transactional(pr ...