解题报告 之 CodeForces150A Win or Freeze

解题报告 之 CodeForces150A Win or Freeze

Description

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 cin, cout streams or the %I64dspecificator.

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.

Sample Input

Input

6

Output

2

Input

30

Output

1
6

Input

1

Output

1
0

题目大意:(吐槽:最近不知道为何感觉都读不太懂题。) 很明显的博弈论,简单的Nim游戏。有一个数字,两个人轮流取它的某因数(不一定),不能是1或者这个数本身,然后将数除以这个因数,先让这个数变为1的人获胜。现在给你一个数字,问你是哪个人获胜,如果是第一个人获胜请输出他第一手取之后给对手剩下的数是多少(任意一组解)?

分析:这个题先把数分解了,看有多少个质数因子(重复的也要算进去),如果质因子(不包含数本身)数量>=3,那么就是1胜利,因为他任意给对手2留下两个质因子的乘积,那么1一定胜利。或者这个数本身是质数,那么也是1直接胜利。一开始没读懂题,最后终于搞清楚了:先判断是不是素数,是的话输出1,0;如果不是,则打素数表(素数筛法),然后分解大数,找到两个质因子就退出,看剩下的数是不是1(是1表示仅有两个因子,此时2胜利),否则1胜利,给对手留下这两个素数的乘积即可。

上代码:

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

int isprime[4000000];
long long prime[2000000];
int cnt = 0;
int fac = 1;

void getprime()
{

	for (int i = 2; i < 4000000; i++)
		isprime[i] = 1;
	prime[1] = 0;
	for (int i = 2; i < 4000000; i++)
	{
		if (!isprime[i]) continue;
		for (int j = i * 2; j < 4000000; j += i)
			isprime[j] = 0;
		prime[cnt++] = i;
	}
}

int isPrime(long long n)
{
	if (n < 4000000) return isprime[n];

	for (int i = 0; prime[i] * prime[i] <= n; i++)
	if (n%prime[i] == 0) return 0;

	return 1;
}

int main()
{
	long long num;
	getprime();
	while (cin >> num)
	{
		int sum = 0;
		fac = 1;
		long long tem = num;

		if (isPrime(num) || num == 1)
		{
			cout << 1 << endl << 0 << endl;
			continue;
		}

		for (int i = 0; i<cnt; i++)
		{
			if (num%prime[i] == 0)
			{
				while (num%prime[i] == 0)
				{

					if (sum <= 2) fac *= prime[i];
					sum++;
					num /= prime[i];
					if (sum >= 2) break;
				}
			}
			if (sum >= 2) break;
		}

		if (sum >=2&&num!=1)
		{
			cout << 1 << endl;
			cout << fac << endl;

		}
		else
			cout << 2 << endl;
	}
	return 0;
}

不过有一种随机化很吊的判断素数和大数分解法,会敲模板但是还不知道原理。

于是乎先摆在这吧明天再学习原理。

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

const int times = 50;
int number = 0;
int fac = 1;
map<long long, int>m;

long long Random(long long n)
{
	return ((double)rand() / RAND_MAX*n + 0.5);
}

long long multi(long long a, long long b, long long mod)
{
	long long ans = 0;
	while (b)
	{
		if (b & 1)
		{
			b--;
			ans = (ans + a) % mod;
		}
		else
		{
			b /= 2;
			a = (a + a) % mod;
		}
	}
	return ans;
}

long long Pow(long long a, long long b, long long mod)
{
	long long ans = 1;
	while (b)
	{
		if (b & 1)
		{
			b--;
			ans = multi(ans, a, mod);
		}
		else
		{
			b /= 2;
			a = multi(a, a, mod);
		}
	}
	return ans;
}

bool witness(long long a, long long n)
{
	long long d = n - 1;
	while (!(d & 1))
		d >>= 1;
	long long t = Pow(a, d, n);
	while (d != n - 1 && t != 1 && t != n - 1)
	{
		t = multi(t, t, n);
		d <<= 1;
	}
	return t == n - 1 || d & 1;
}

bool miller_rabin(long long n)
{
	if (n == 2)
		return true;
	if (n<2 || !(n & 1))
		return false;
	for (int i = 1; i <= times; i++)
	{
		long long a = Random(n - 2) + 1;
		if (!witness(a, n))
			return false;
	}
	return true;
}

long long gcd(long long a, long long b)
{
	if (b == 0)
		return a;
	return gcd(b, a%b);
}

long long pollard_rho(long long n, long long c)
{
	long long x, y, d, i = 1, k = 2;
	x = Random(n - 1) + 1;
	y = x;
	while (1)
	{
		i++;
		x = (multi(x, x, n) + c) % n;
		d = gcd(y - x, n);
		if (1<d&&d<n)
			return d;
		if (y == x)
			return n;
		if (i == k)
		{
			y = x;
			k <<= 1;
		}
	}
}

void find(long long n, long long c)
{
	if (n == 1)
		return;
	if (miller_rabin(n))
	{
		m[n]++;
		number++;
		if (number <= 2) fac *= n;
		return;

	}
	long long p = n;
	while (p >= n)
		p = pollard_rho(p, c--);
	find(p, c);
	find(n / p, c);
}

int main()
{
	long long n;
	while (cin>>n)
	{
		m.clear( );
		number = 0;
		fac = 1;
		find(n, 2013729);
		/*
		map<long long, int>::iterator it = m.begin();
		for (; it != m.end( ); it++)
			cout << it->first << endl;
		*/

		if (n==1||miller_rabin(n))
		{
			cout << 1 << endl << 0 << endl;;
			continue;
		}

		if (number > 2 )
		{
			cout << 1 << endl;
			cout << fac;
			cout << endl;
		}
		else
			cout << 2 << endl;

	}

	return 0;
}

感觉很吊。。

时间: 2024-10-29 17:03:55

解题报告 之 CodeForces150A Win or Freeze的相关文章

解题报告 之 WHU1124 Football Coach

解题报告 之 WHU1124 Football Coach Description It is not an easy job to be a coach of a football team. The season is almost over, only a few matches are left to play. All of sudden the team manager comes to you and tells you bad news: the main sponsor of

解题报告 之 POJ3057 Evacuation

解题报告 之 POJ3057 Evacuation Description Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了

【百度之星2014~初赛(第二轮)解题报告】Chess

声明 笔者最近意外的发现 笔者的个人网站http://tiankonguse.com/ 的很多文章被其它网站转载,但是转载时未声明文章来源或参考自 http://tiankonguse.com/ 网站,因此,笔者添加此条声明. 郑重声明:这篇记录<[百度之星2014~初赛(第二轮)解题报告]Chess>转载自 http://tiankonguse.com/ 的这条记录:http://tiankonguse.com/record/record.php?id=667 前言 最近要毕业了,有半年没做

2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告

2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh童鞋的提醒. 勘误2:第7题在推断连通的时候条件写错了,后两个if条件中是应该是<=12 落了一个等于号.正确答案应为116. 1.煤球数目 有一堆煤球.堆成三角棱锥形.详细: 第一层放1个, 第二层3个(排列成三角形), 第三层6个(排列成三角形), 第四层10个(排列成三角形). -. 假设一共

[noip2011]铺地毯(carpet)解题报告

最近在写noip2011的题,备战noip,先给自己加个油! 下面是noip2011的试题和自己的解题报告,希望对大家有帮助,题目1如下 1.铺地毯(carpet.cpp/c/pas) [问题描述]为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有n 张地毯,编号从1 到n.现在将这些地毯按照编号从小到大的顺序平行于坐标轴先后铺设,后铺的地毯覆盖在前面已经铺好的地毯之上.地毯铺设完成后,组织者想知道覆盖地面某个点的最上面的那张地毯的

ACdream 1203 - KIDx&#39;s Triangle(解题报告)

KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description One day, KIDx solved a math problem for middle students in seconds! And than he created this problem. N

解题报告 之 CodeForces 91B Queue

解题报告 之 CodeForces 91B Queue Description There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue.

解题报告 之 POJ1226 Substrings

解题报告 之 POJ1226 Substrings Description You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings. Input The first li