POJ2965(找规律)题解

POJ2965(找规律)题解

题目

The Pilots Brothers’ refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 35766 Accepted: 13828
Description
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.
The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

题意

这题和POJ1753差不多,只是每一次改变影响的范围变成了行和列的所有点,最终目的是全部变为“-”。

分析

一开始,我直接套用的我之前写的 POJ1753(枚举)的代码,更改了翻转函数,加了个string用于存储翻转过程中的i和j。结果超时了,但最多只有65535次运算,不知道为毛会超时。
后来转换思路,发现了规律,想要将一个点s的“+”变成“-”,只要将s所在的行和列的所有点都翻转一遍就能实现。因为整个过程中只有s点翻转了奇数次,s所在的行和列的其他点都翻转了偶数次,等于没变化。所以我们只需要遍历矩阵,每次遇到“+”就翻转改点所在的行和列的所有点一次。统计所有点翻转的次数,如果是奇数则要翻转,偶数则不用。输出所有要翻转的点。

代码

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	int g[7][7] = {0};
	for (int i = 1; i <= 4; i ++ )
	{
		for (int j = 1; j <= 4; j ++ )
		{
			char c;
			cin >> c;
			if (c == '+')
			{
				for (int k = 1; k <= 4; k ++ )
				{
					g[i][k] ++;
					g[k][j] ++;
				}
				g[i][j] --;
			}
		}
	}
	int res = 0;
	for (int i = 1; i <= 4; i ++ )
	{
		for (int j = 1; j <= 4; j ++ )
		{
			if (g[i][j] & 1) res ++ ;
		}
	}
	cout << res << endl;
	for (int i = 1; i <= 4; i ++ )
	{
		for (int j = 1; j <= 4; j ++ )
		{
			if (g[i][j] & 1) cout << i << " " << j << endl;
		}
	}
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
POJ3635是一道经典的数学题,需要使用一些数学知识和算法进行解决。 题目描述: 给定四个正整数 a、b、p 和 k,求 a^b^p mod k 的值。 解题思路: 首先,我们可以将指数 b^p 写成二进制形式:b^p = c0 * 2^0 + c1 * 2^1 + c2 * 2^2 + ... + ck * 2^k,其中 ci 为二进制数的第 i 位。 然后,我们可以通过快速幂算法来计算 a^(2^i) mod k 的值。具体来说,我们可以用一个变量 x 来存储 a^(2^i) mod k 的值,然后每次将 i 加 1,如果 ci 为 1,则将 x 乘上 a^(2^i) mod k,最后得到 a^b^p mod k 的值。 代码实现: 以下是 Java 的代码实现: import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); BigInteger a = sc.nextBigInteger(); BigInteger b = sc.nextBigInteger(); BigInteger p = sc.nextBigInteger(); BigInteger k = sc.nextBigInteger(); BigInteger ans = BigInteger.ONE; for (int i = 0; i < p.bitLength(); i++) { if (b.testBit(i)) { ans = ans.multiply(a.modPow(BigInteger.ONE.shiftLeft(i), k)).mod(k); } } System.out.println(ans); } } 其中,bitLength() 函数用于获取二进制数的位数,testBit() 函数用于判断二进制数的第 i 位是否为 1,modPow() 函数用于计算 a^(2^i) mod k 的值,multiply() 函数用于计算两个 BigInteger 对象的乘积,mod() 函数用于计算模数。 时间复杂度: 快速幂算法的时间复杂度为 O(log b^p),其中 b^p 为指数。由于 b^p 的位数不超过 32,因此时间复杂度为 O(log 32) = O(1)。 总结: POJ3635 是一道经典的数学题,需要使用快速幂算法来求解。在实现时,需要注意 BigInteger 类的使用方法,以及快速幂算法的细节。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微光曾经闪烁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值