CF1370C Number Game (Game Theory + Number Theory)

topic

At the beginning, we can put nnn is divided into three cases

  • When n = 1 n=1n=1 , the second hand wins
  • When nnn is odd, win first,nnJust divide n by itself

The first two cases are relatively simple, let's discuss when nnWhen n is an even number,
whennnn is an even number (2 2Multiples of 2 ), if you want to win, you definitely can’t use minus1 11 operation, because the second player will win. Then consider dividing by the divisor
becausennn is an even number, we can definitely putnndecompose n into some prime numbers and2 2The product of positive integer powers of 2 n = p 1 × p 2 ×… × pm × 2 kn=p_1 \times p_2 \times \cdots \times p_m \times 2^kn=p1×p2××pm×2k ( k > 0 , m ≥ 0 ) (k>0,m \ge 0) (k>0,m0 ) , and these prime numbers are not2 2A multiple of 2.
Because the value removed each time can be the product of multiple prime factors (as long as it is a divisor), as long asm> 1 m>1m>1 ,Ashishgupyou can choose the approximate number flexibly.
Finally, whennnn is removed, only2 k 2^k2When k , there will be two situations

  1. n = 2 n=2 n=2. The person who takes first wins: judge whether themmm prime numbers are converted into even numbers that are not1 1A product of the number of, if possible, this descriptionAshishgupcan take the first (Ashishgupwin)
  2. n > 2 n>2 n>2. The winner wins: judge whether themmm prime numbers are converted to odd numbers, not1 1A product of the number of, if possible, this descriptionAshishgupmay be taken after (Ashishgupwinning)

Otherwise, the winner is FastestFingerthe matter

Code

#include<cstdio>
#include<iostream>
using namespace std;
int n;
int main()
{
    
    
//	freopen("in.txt","r",stdin);
	int T;
	scanf("%d",&T);
	while(T--)
	{
    
    
		scanf("%d",&n);
		if(n==1){
    
    puts("FastestFinger");continue;}
		if((n & 1) || n==2){
    
    puts("Ashishgup");continue;}
		int tot=0;
		for(int i=2;i*i<=n;++i)
		if((i & 1) && n%i==0) // 注意这里只能把为奇数的质因数
		while(n%i==0)++tot,n/=i;
		for(int i=2;i*i<=n;++i)
		{
    
    
			int tmp=n/i;
			if((tmp & 1) && n%i==0)
			while(n%tmp==0)++tot,n/=tmp;
		}
		if(n==2)
		{
    
    
			if(tot!=1)puts("Ashishgup");
			else puts("FastestFinger"); // 如果只有一个质数的话就不能化为偶数个数之积
		}
		else
		{
    
    
			if(tot)puts("Ashishgup");
			else puts("FastestFinger"); // 这里特判的是0,跟上面原理相同
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/Brian_Pan_/article/details/106993264