Topic description
The game starts with a target integer N≥2, and an integer M = 1. Alice and Bob take alternate turns. At each turn, the player chooses a prime divisor p of N, and multiply M by p. If the player’s move makes the value of M equal to the target N, the player wins. If M > N, the game is a tie.
Assuming that both players play optimally, who (if any) is going to win?
enter
output
sample input
10
10 Alice
20 Bob
30 Alice
40 Bob
50 Alice
60 Bob
70 Alice
80 Bob
90 Alice
100 Bob
Sample output
Bob
Bob
tie
tie
Alice
tie
tie
tie
tie
Alice
The main idea of the title: Two people take turns multiplying the prime factors of the given numbers. The one who reaches the given number first wins, and a draw can occur, that is, someone’s decision can only be in a sure-defeat state, and the game programming will draw a draw through operations (I can’t. won't let you win) amazing
Analysis of ideas: According to the unique solution theorem, the number of prime factors can be divided into three situations: there is only one prime factor, there are two kinds, and there are more than three kinds.
(1) There is only one type of prime factor: if the prime factor needs an odd number, the first player wins, and if an even number is required, the second player wins;
(2) There are two kinds of prime factors: if the required number of the two prime factors is the same, the second hand wins (no matter how the first hand operates, the second hand only needs to be balanced);
The required numbers of the two prime factors are different. If the difference is one, the first mover wins; otherwise, the original must lose state can be tied by manipulation;
(3) There are three or more prime factors: it must be possible to make a draw by manipulation.
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 const int mod = 1e9+7; 5 typedef long long ll; 6 #define LL long long 7 8 const int maxn = 1e5+100; 9 10 int prime[maxn+1]; 11 12 bool vis[maxn]; 13 ll cnt; 14 void primejudge(int n) 15 { 16 cnt=0; 17 vis[1]=true; 18 ll i,j; 19 for(i=2; i<=n; i++) 20 { 21 if(!vis[i]) 22 { 23 prime[cnt++]=i; 24 } 25 for(j=0; j<cnt&&i*prime[j]<=n; j++) 26 { 27 vis[i*prime[j]]=true; 28 if(i%prime[j]==0) 29 { 30 break; 31 } 32 } 33 } 34 } 35 36 int main() 37 { 38 primejudge(maxn); 39 int T; 40 cin>>T; 41 while(T--) 42 { 43 ll n; 44 string ss; 45 cin>>n>>ss; 46 47 int ret = 0; 48 vector<int>v; 49 for(int i=0; i<cnt; i++) 50 { 51 52 if(n%prime[i]==0) 53 { 54 ret++; 55 int tmp=0; 56 while(n%prime[i]==0) 57 { 58 n/=prime[i]; 59 tmp++; 60 } 61 v.push_back(tmp); 62 } 63 if(ret>=3) 64 break; 65 } 66 if(n>1) 67 { 68 ret++; 69 v.push_back(1); 70 } 71 if(ret>=3) 72 { 73 cout<<"tie"<<endl; 74 } 75 else if(ret==1) 76 { 77 78 if(v[0]%2==0) 79 { 80 if(ss=="Alice") 81 cout<<"Bob"<<endl; 82 else 83 cout<<"Alice"<<endl; 84 } 85 else 86 { 87 if(ss!="Alice") 88 cout<<"Bob"<<endl; 89 else 90 cout<<"Alice"<<endl; 91 } 92 } 93 else if(ret==2) 94 { 95 96 //cout<<" 2 "<<endl; 97 if(v[1]==v[0]) 98 { 99 if(ss=="Alice") 100 cout<<"Bob"<<endl; 101 else 102 cout<<"Alice"<<endl; 103 } 104 else 105 { 106 if(abs(v[0]-v[1])==1) 107 if(ss!="Alice") 108 cout<<"Bob"<<endl; 109 else 110 cout<<"Alice"<<endl; 111 else 112 { 113 cout<<"tie"<<endl; 114 } 115 } 116 } 117 else 118 { 119 cout<<ss<<endl; 120 } 121 } 122 123 }
Note: The teammates started this question a few times. First, the linear sieve of the data is 1e9 and cannot preprocess all prime factors, but it can be considered to preprocess only the prime numbers within 1e5, because after more than 1e5, there is at most and only one prime factor, otherwise itself;
However, there is still a special case that needs to be considered when counting prime factors. Let’s give an example: if the given number is 10000019, which is a prime number but greater than 1e5, you can directly ret++, but 2*10000019 needs to be counted first when counting prime factors. 2 this factor, then because
The quotient is not equal to one, ret++, and v.push_back(1). Why do you want to push, because you want to compare the number of occurrences of the prime factor, because this number is greater than 1e5, so it appears once later, so push_back(1).