我看大多数人的博客只说了一句:找规律得答案为(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. Careercup - Facebook面试题 - 5179916190482432

    2014-05-01 00:45 题目链接 原题: input [,,,] output [,,,] Multiply all fields except it's own position. Res ...

  2. 2391: Cirno的忧郁 - BZOJ

    Description Cirno闲着无事的时候喜欢冰冻青蛙.Cirno每次从雾之湖中固定的n个结点中选出一些点构成一个简单多边形,Cirno运用自己的能力能将此多边形内所有青蛙冰冻.雾之湖生活着m只 ...

  3. js异步加载 defer和async 比较

    网上说法很多,很少一句话能总结清楚的,终于找到两句一针见血的描述,很到位: 相同点:都不阻塞DOM解析 defer  :顺序:保证先后顺序.解析:HTML 解析器遇到它们时,不阻塞(脚本将被异步下载) ...

  4. 剑指offer--面试题12

    题目:打印从1~最大的n位数 分析:知道陷阱在哪,即n很大时若用通常的int,long会溢出:想到用字符串解决,这涉及到字符转数字及反过来. 刚开始纠结于字符串怎么加1,想了片刻,觉得应该取出最后一位 ...

  5. AlphaToCoverage solution

    After msaa output the alpha in ps remove clip in ps in blendstate add AlphaToCoverageEnable = TRUE - ...

  6. PHP之session相关实例教程与经典代码

    ·php 中cookie和session的用法比较 ·phpmyadmin报错:Cannot start session without errors问题 ·php中cookie与session应用学 ...

  7. android 解析XML方式(三)

    上一节中,我们使用SAX方式解析xml文档, SAX方式是基于事件驱动的.当然android的事件机制是基于回调函数的.在这一节中,我们用另外一种方式解析xml文档,这种方式也是基于事件驱动的,与SA ...

  8. IOS 视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear等的区别及用途

    iOS视图控制对象生命周期-init.viewDidLoad.viewWillAppear.viewDidAppear.viewWillDisappear.viewDidDisappear的区别及用途 ...

  9. Project Euler 89:Roman numerals 罗马数字

    Roman numerals For a number written in Roman numerals to be considered valid there are basic rules w ...

  10. [hackerrank]Even Odd Query

    https://www.hackerrank.com/contests/w5/challenges 简单题,注意整数的0次方是1,奇数. #include <iostream> #incl ...