UVA - 11774 Doom's Day (规律)

We all know about the legend oftower of Hanoi. It is said that the world will end after finishing the puzzle.What we don‘t know is another legend about when the world will end which is verifiedby the scientists.

It is all about a 3^n * 3^m grid.Initially the grid is filled with 1 to 3^(m+n) in row major order. At each stepthe puzzle is rearranged by reading it in row major order and putting them incollumn major order. See the following examples.


1


2


3


to


1


10


19


4


5


6


2


11


20


7


8


9


3


12


21


10


11


12


4


13


22


13


14


15


5


14


23


16


17


18


6


15


24


19


20


21


7


16


25


22


23


24


8


17


26


25


26


27


9


18


27


1


2


3


to


1


4


7


4


5


6


2


5


8


7


8


9


3


6


9

Now every day the puzzle is rearrangedonce. The legend says if someday initial configuration returns the world willend. Now you are wondering when the world is going to end.

Input

Input starts with a linecontaining number of test cases T ≤ 10000. Each test case containstwo positive integer
m ≤  10^9and n≤ 10^9.

Output

For each case print one linecontaining days before dooms day. The input will be such that this number fitsin 64 bit unsigned integer.

SampleInput                                                 Outputfor Sample Input


5

1 1

1 2

3 1

2 2

98876767 12234


Case 1: 2

Case 2: 3

Case 3: 4

Case 4: 2

Case 5: 98889001

Problemsetter:Tanaeem Md. Moosa

SpecialThanks to: Jane Alam Jan

题意:往3^n * 3^m的格子里1到3^(n+m) 的数,每次的操作是:将数按行优先拿出来,再按列优先放进去,求多少次后回到初始状态

思路:规律ans=(a+b)/gcd(a, b);

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}

int main() {
	int t, a, b, cas = 1;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &a, &b);
		printf("Case %d: %d\n", cas++, (a+b)/gcd(a, b));
	}
	return 0;
}

UVA - 11774 Doom's Day (规律)

时间: 2024-10-05 10:57:18

UVA - 11774 Doom's Day (规律)的相关文章

UVA 11774 - Doom&#39;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

UVA 11774 - Doom&amp;#39;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 cel

UVA 10479 The Hendrie Sequence 规律

题目大意:一个序列,刚开始由0变到了1,接着往后一个个变化下去 变化的规则是,如果当前数是k,就在这个序列的最后面加上k-1个0,再加上k+1 现在问这个序列的第n个数是多少 解题思路:这是有规律的,第2的k次方个数刚好是k 如果当前数是k,且k刚好是2的n次方,那么这个数前面就有n-1个0,n-2个1,n-3个02组合,以此类推 如果要求第n个数是多少,只需要找到n是哪个k之前的,然后依照上面的规律依次递归下去即可 #include<cstdio> #include<cstring&g

UVa 1620 Lazy Susan (找规律)

题意:给 n 个数,每次可以把4个连续的数字翻转,问你能不能形成1-n的环状排列. 析:找一下奇偶性,写几个数试试,就会找到规律. 代码如下: #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <qu

UVa 12627 (递归 计数 找规律) Erratic Expansion

直接说几个比较明显的规律吧. k个小时以后,红气球的个数为3k. 单独观察一行: 令f(r, k)为k个小时后第r行红气球的个数. 如果r为奇数,f(r, k) = f((r+1)/2, k-1) * 2 如果r为偶数,f(r, k) = f(r/2, k-1) 令g(r, k)为k个小时后前r行红气球的个数. 如果r为偶数,g(r, k) = g(r/2, k-1) * 3; 如果r为奇数,g(r, k) = g(r-1, k) + f(r, k); 因此f和g都可以用递归求解. 1 #inc

UVA - 10120 Gift?! 暴力+规律

题目大意:有n块石头,礼物在第m块石头上,相邻石头的距离为1,规定小青蛙第一步跳到第一块石头上,接下来的跳跃要符合该规则,假设这是第n次跳跃,那么小青蛙跳跃的距离为(2 * n - 1),且每次跳跃都必须跳到石头上 解题思路:石头数量如果超过49的话,小青蛙就可以跳到任意一块石头上,其他的情况只需暴力dfs就可以解决了 #include<cstdio> #include<cstring> int pos, len; bool dfs(int cur, int time) { if(

Doom 规律+大数

Doom 比赛的时候没有做出来,补题. 题意:题目定义了一个斐波那契串 1) fib1=b; 2) fib2=a; 3) fibi=fibi-1fibi-2,i>2 举例,fib3=ab,fib4=aba,fib5=abaab 我们暂时将字符串sisi+1si+2si+3…sj记做s[i:j] 求满足s[1:i]=s[m-i+1:m](i<m)的i的最大值,记做LBorderm 例如m=5时,LBorderm=2,因为abaab中前两个和末尾两个相同,即黑色部分 解题思路: 一看到题目的数据这

UVA 1363 Joseph&#39;s Problem 找规律+推导 给定n,k;求k%[1,n]的和。

/** 题目:Joseph's Problem 链接:https://vjudge.net/problem/UVA-1363 题意:给定n,k;求k%[1,n]的和. 思路: 没想出来,看了lrj的想法才明白. 我一开始往素数筛那种类似做法想. 想k%[1,n]的结果会有很多重复的,来想办法优化. 但没走通. 果然要往深处想. 通过观察数据发现有等差数列.直接观察很难确定具体规律:此处应该想到用式子往这个方向推导试一试. lrj想法: 设:p = k/i; 则:k%i = k-i*p; 容易想到

(白书训练计划)UVa 12627 Erratic Expansion(递归+找规律)

题目地址:UVa 12627 这题是先找规律,规律在于对于第k个小时的来说,总是可以分成右下角全是蓝色气球,右上角,左下角与左上角三个一模一样的k-1个小时的气球.这样的话,规律就很清晰了,然后用递归做比较方便... 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <ma