codeforces 151C Win or Freeze

本文介绍了一款在寒冷冬天进行的特殊游戏,玩家通过找出特定数字的非平凡因数来进行比赛,文章详细解析了游戏的获胜策略,并通过素因子分解的方法来确定首位玩家是否能够获胜及如何获胜。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Win or Freeze
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You can't possibly imagine how cold our friends are this winter in Nvodsk! Two of them play the following game to warm up: initially a piece of paper has an integer q. During a move a player should write any integer number that is a non-trivial divisor of the last written number. Then he should run this number of circles around the hotel. Let us remind you that a number's divisor is called non-trivial if it is different from one and from the divided number itself.

The first person who can't make a move wins as he continues to lie in his warm bed under three blankets while the other one keeps running. Determine which player wins considering that both players play optimally. If the first player wins, print any winning first move.

Input

The first line contains the only integer q (1 ≤ q ≤ 1013).

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cincout streams or the%I64d specificator.

Output

In the first line print the number of the winning player (1 or 2). If the first player wins then the second line should contain another integer — his first move (if the first player can't even make the first move, print 0). If there are multiple solutions, print any of them.

Examples
input
6
output
2
input
30
output
1
6
input
1
output
1
0
Note

Number 6 has only two non-trivial divisors: 2 and 3. It is impossible to make a move after the numbers 2 and 3 are written, so both of them are winning, thus, number 6 is the losing number. A player can make a move and write number 6 after number 306, as we know, is a losing number. Thus, this move will bring us the victory.



将q素因子分解,然后统计素因子的个数,如果素因子个数大于2,那么先手只要拿得只剩下2个给后手,那么先手必胜,因为后手不论怎么拿,先手都不可分了。

如果素因子个数等于2,那么先手必败。

如果素因子个数为1或者q本身为1,那么先手直接取胜。

可以用素数筛先打个[2, sqrt(q)]的表,然后计算素因子个数,对于最后的提取完[2, sqrt(q)]中素因子的q要么为1要么为一个素数,若是素数的话素因子个数还要加一


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;
const ll maxn = 10000003;

ll q, num[10000005], len, cnt;
bool primer[10000005];

int main()
{
	cin >> q;
	if (q == 1) {
		printf("1\n0\n");
		return 0;
	}
	primer[0] = primer[1] = true;
	len = 0;
	for (ll i = 2; i <= maxn; i++) {
		if (!primer[i]) {
			num[len++] = i;
			for (ll j = 2; j * i <= maxn; j++) {
				primer[i * j] = true;
			}
		}
	}
	cnt = 0;
	ll x = 1, y = 1, tt = q;
	for (ll i = 0; i < len; i++) {
		if (q % num[i] == 0) {
			cnt++;
			q /= num[i];
			if (cnt == 1) x = num[i];
			else if (cnt == 2) y = num[i];
			i--;
		}
	}
	//如果最后q没有除尽到1,说明最后q也是个素数
	if (q != 1) cnt++;
	if (cnt == 1) {
		printf("1\n0\n");
	} else if (cnt == 2) {
		puts("2");
	} else {
		printf("1\n%I64d\n", x * y);
	}
	return 0;
}



### 关于 Floor 和 Ceil 函数在编程中的实现 Floor 和 Ceil 是两个常见的数学函数,在许多算法竞赛平台(如 Codeforces)上经常被用于解决涉及整数除法、取模运算等问题。以下是它们的定义以及如何在程序设计中实现这些功能。 #### 定义 - **Floor Function**: 对实数向下取整,返回不大于该数值的最大整数[^1]。 - **Ceil Function**: 对实数向上取整,返回不小于该数值的最小整数[^2]。 #### 实现方法 大多数现代编程语言都提供了内置库来支持 floor 和 ceil 的计算: ##### 使用标准库 在 C++ 中可以利用 `<cmath>` 库中的 `floor` 和 `ceil` 方法完成相应操作: ```cpp #include <iostream> #include <cmath> int main() { double num = 3.7; std::cout << "Floor value: " << static_cast<int>(std::floor(num)) << "\n"; // 输出 3 std::cout << "Ceil value: " << static_cast<int>(std::ceil(num)) << "\n"; // 输出 4 } ``` 然而需要注意的是,当处理负数时,这两个函数的行为可能不符合直觉。例如,`floor(-2.5)` 返回 `-3` 而不是 `-2`,因为它是严格意义上的“下限”。 如果目标是在没有浮点误差的情况下仅针对正整数执行类似的逻辑,则可以通过简单的算术表达式手动模拟这两种行为而无需调用任何外部库函数: - 手动实现 Floor: 当 a / b 结果为非零余数时保持原样;否则减去一个小量使得最终结果总是趋向更小的方向移动直到达到下一个较低的整数位置为止。 - 手动实现 Ceil: 如果存在剩余部分则增加到下一更高的整数值上去;如果没有多余的部分,那么它本身已经是天花板高度了. 具体代码如下所示: ```python def manual_floor(a, b): return (a // b) def manual_ceil(a, b): if a % b == 0: return a // b else: return (a // b) + 1 ``` 上述 Python 版本展示了如何通过基本运算符构建自己的版本而不依赖额外模块的帮助[^3]. ### 示例应用案例分析 考虑到实际应用场景下的复杂度需求,这里选取几个典型例子加以说明其用途所在之处及其重要性何在? 比如 CF 圆形 #701(Division Two)-Problem C(Floor And Mod), 这道题目要求我们找到满足特定条件的一系列数字组合方案数目统计工作当中需要用到大量的地板除法规律推导过程才能得出正确结论出来. 同样还有像CF Problem D(编号1469)-Ceiling Division Operations [^2], 此处探讨的就是怎样快速有效地把初始状态转变成为期望结束形态所经历过的最少次数动作规划策略研究方向上面去了.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值