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(规律)

时间: 2024-10-10 22:08:30

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

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

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