UVA 11774 - Doom's Day(规律)
UVA 11774 - Doom's Day
题意:给定一个3^n*3^m的矩阵,要求每次按行优先取出,按列优先放回,问几次能回复原状
思路:没想到怎么推理,找规律答案是(n + m) / gcd(n, m),在topcoder上看到一个证明,例如以下:
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).
然后看了之后还是不理解啊,有哪个大神理解这个推理过程求指导一下。。
代码:
#include <stdio.h>
#include <string.h> int t;
long long n, m; long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
} int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
scanf("%lld%lld", &n, &m);
printf("Case %d: %lld\n", ++cas, (n + m) / gcd(n, m));
}
return 0;
}
UVA 11774 - Doom's Day(规律)的更多相关文章
- UVA - 11774 Doom's Day
看样例猜结论hhhhhh,竟然蒙对了..(正确性待证明) #include<bits/stdc++.h> #define ll long long using namespace std; ...
- UVa 11774 (置换 找规律) Doom's Day
我看大多数人的博客只说了一句:找规律得答案为(n + m) / gcd(n, m) 不过神题的题解还须神人写.. We can associate at each cell a base 3-numb ...
- 紫书 习题 10-13 UVa 11526(打表找规律+分步枚举)
首先看这道题目,我预感商数肯定是有规律的排列的,于是我打表找一下规律 100 / 1 = 100 100 / 2 = 50 100 / 3 = 33 100 / 4 = 25 100 / 5 = ...
- UVa 102 - Ecological Bin Packing(规律,统计)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- UVA 816 - Abbott's Revenge(BFS)
UVA 816 - Abbott's Revenge option=com_onlinejudge&Itemid=8&page=show_problem&category=59 ...
- UVa 808 (建坐标系、找规律) Bee Breeding
题意: 如图,按照图中的规律给这些格子编号.给出两个格子的编号,求从一个格子到另一个格子的最少步数.(一步只能穿过有有公共边的格子) 分析: 根据高中数学知识,选任意两个不共线的向量,就能表示平面上所 ...
- uva 11825 Hackers' Crackdown (状压dp,子集枚举)
题目链接:uva 11825 题意: 你是一个黑客,侵入了n台计算机(每台计算机有同样的n种服务),对每台计算机,你能够选择终止一项服务,则他与其相邻的这项服务都终止.你的目标是让很多其它的服务瘫痪( ...
- UVA 10831 - Gerg's Cake(数论)
UVA 10831 - Gerg's Cake 题目链接 题意:说白了就是给定a, p.问有没有存在x^2 % p = a的解 思路:求出勒让德标记.推断假设大于等于0,就是有解,小于0无解 代码: ...
- UVA 12103 - Leonardo's Notebook(数论置换群)
UVA 12103 - Leonardo's Notebook 题目链接 题意:给定一个字母置换B.求是否存在A使得A^2=B 思路:随意一个长为 L 的置换的k次幂,会把自己分裂成gcd(L,k) ...
随机推荐
- FreeNAS 9.1.1 发布,网络存储系统 - 开源中国社区
FreeNAS 9.1.1 发布,网络存储系统 - 开源中国社区 FreeNAS 9.1.1 发布,网络存储系统
- cocostudio——js 3 final控件事件
近期试用了下cocos ide,然后引擎用的cocos2dx js 3 final,须要build runtime一下,以下是cocos studio相关的一些事件: 加入事件侦听: // butto ...
- poj 动态规划的主题列表和总结
此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...
- 2014 CSDN博文大赛终于获奖名单发布
博文大赛第二阶段(2014年7月15日-2014年8月10日)已经结束,决赛获奖名单已在8月11日出炉. 现将获奖名单发布: 移动开发 NO.1 罗升阳 Luoshengyang S ...
- (7)基于hadoop的简单网盘应用实现3
一.login.jsp登陆界面实现 解压bootmetro-master.zip,然后将\bootmetro-master\src\下的assets目录复制到project里. bootmetro下载 ...
- windows 2003 自动安全设置
@echo offecho.echo.echo.echo 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓echo.echo.echo windows 2003 自动安全设置程序 echo. ec ...
- Android SDK r23
SDK Tools Only Platform Package Size MD5 Checksum Windows 32 & 64-bit android-sdk_r23-windows.zi ...
- Python内置函数str()和repr()
内建函数str()和repr() (representation.表达,表示)或反引號操作符(``)能够方便地以字符串的方式获取对象的内容.类型.数值属性等信息. str()函数得到的字符串可读性好( ...
- SESC中的热量模拟器
SESC安装见前文 配置sesc支持热量模拟 ../sesc/configure --enable-power --enable-therm make 遇到问题: 1 找不到 liblevmar.a ...
- JSTL自定义标签库 (二)
要定义自己的标签,首先写个java类,extends TagSupport 或者 implements Tag ,然后在类体里实现自己想要的方法,或者覆盖父类的方法. 我定义的MyTag代码如下: ...