UVa 11774 (置换 找规律) Doom's Day
我看大多数人的博客只说了一句:找规律得答案为(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+rWe 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^4now : 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的更多相关文章
- 紫书 习题8-5 UVa 177 (找规律)
参考了https://blog.csdn.net/weizhuwyzc000/article/details/47038989 我一开始看了很久, 拿纸折了很久, 还是折不出题目那样..一脸懵逼 后来 ...
- 紫书 习题 8-20 UVa 1620 (找规律+求逆序对)
这道题看了半天没看出什么规律, 然后看到别人的博客, 结论是当n为奇数且逆序数为奇数的时候 无解, 否则有解.但是没有给出证明, 在网上也找到详细的证明--我也不知道是为什么-- 求逆序对有两种方法, ...
- 紫书 例题8-12 UVa 12627 (找规律 + 递归)
紫书上有很明显的笔误, 公式写错了.g(k, i)的那个公式应该加上c(k-1)而不是c(k).如果加上c(k-1)那就是这一次 所有的红气球的数目, 肯定大于最下面i行的红气球数 我用的是f的公式, ...
- 紫书 例题 10-25 UVa 1363(找规律)
可以发现余数是成一段一段的等差数列的. 在商数同的时候,余数是成首项为第一个数的余数,公差 为商数的等差数列. 利用这个性质求解即可. #include<cstdio> #include& ...
- UVA 11774 - Doom's Day(规律)
UVA 11774 - Doom's Day 题目链接 题意:给定一个3^n*3^m的矩阵,要求每次按行优先取出,按列优先放回,问几次能回复原状 思路:没想到怎么推理,找规律答案是(n + m) / ...
- 【数论,找规律】Uva 11526 - H(n)
原来做过的题再看还是没想出来,看来当时必然没有真正理解.这次回顾感觉理解更透彻了. 网上的题解差不多都是一个版本,而且感觉有点扯.根据n=20猜出来的? 好吧哪能根据一个就猜到那么变态的公式.其实这题 ...
- 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 ...
- 递推+高精度+找规律 UVA 10254 The Priest Mathematician
题目传送门 /* 题意:汉诺塔问题变形,多了第四个盘子可以放前k个塔,然后n-k个是经典的汉诺塔问题,问最少操作次数 递推+高精度+找规律:f[k]表示前k放在第四个盘子,g[n-k]表示经典三个盘子 ...
- UVA 10254 - The Priest Mathematician (dp | 汉诺塔 | 找规律 | 大数)
本文出自 http://blog.csdn.net/shuangde800 题目点击打开链接 题意: 汉诺塔游戏请看 百度百科 正常的汉诺塔游戏是只有3个柱子,并且如果有n个圆盘,至少需要2^n- ...
随机推荐
- Ubuntu下安装配置zsh和oh my zsh
zsh优势:自动补全功能强大和很高的可配置性 1.查看当前系统装了哪些shell cat /etc/shells 2.当前正在运行的是哪个版本的shell echo $SHELL 3.安装 ...
- MVC3 ModelBinder
1.Model Binder从哪些地方获取数据(找到数据后会停止继续寻找) MVC 框架内置默认的 Model Binder 是 DefaultModelBinder 类.当 Action Invok ...
- 网站常用css必备css reset
在我们写前端代码页面的时候,很多常用的CSS类都是固定的!但没有一个标准或者大家都按自己的方式去随意的写,这样就每次都重复写一些固定的类! 为此HTML5 Doctor(HTML5医生)为我们总结了一 ...
- Matlab实现二进制矩阵转换为十进制
一.问题描述 [1 1 1 0 1 0 1 1 0 1 0 0 1 1 0] 每两位3转换为一个十进制数,共5列,那么转换后是ceil(5/3)=2列. [7 1 6 1 1 2] 二.问题分析 1. ...
- 17.2 The DispatcherServlet
综述: Spring’s web MVC framework is, like many other web MVC frameworks, request-driven, designed arou ...
- C# 生成MD5编码方法(不同位数)
/// <summary> /// </summary> /// <param name="strSource"& ...
- 使用异步 I/O 大大提高应用程序的性能
使用异步 I/O 大大提高应用程序的性能 学习何时以及如何使用 POSIX AIO API Linux® 中最常用的输入/输出(I/O)模型是同步 I/O.在这个模型中,当请求发出之后,应用程序就会阻 ...
- POJ3087Shuffle'm Up(map)
http://poj.org/problem?id=3087 题意 : 我只能说,,英语不好是硬伤...这个题比较别扭啊,不知道真正题意是不是我所想的,我先把我A了的代码按照的题意的意思说一下,就是说 ...
- 在UWSGI和NGINX配合的情况下,配置个别目录上传及超时
笨办法就是多加一个LOCATION. 然后,自定义以下类似参数 client_max_body_size uwsgi_connect_timeout uwsgi_read_timeout server ...
- [转]Ubuntu alternate和desktop区别
原文地址:http://blog.csdn.net/is2120/article/details/6797621 Desktop : 刻录在光盘,从光盘运行的系统,相当于 Live CD Altern ...