ACdream 1112 Alice and Bob (game & prime number screening optimization)
Question link: Portal
Game rules:
Once again, you can divide a heap into two heap x = a * B (! = 1 & B! = 1) x indicates the number of original heaps, and a and B indicate the number of new heaps.
You can also change the number of the original heap to the approximate number y and y of the original heap! = X. The person who made the last operation wins.
Analysis:
It is also a stone-removing game, so we only need to compare the sg values in all situations.
Let's first consider a bunch of things. Set the number of this heap to x;
In this case
(A1, x/a1), (a2, x/a2 ),..., (an, x/an); or (a1), (a2 ),.., ().
Because the data volume is large, the simple query for an appointment will definitely time out. Then analyze the problem carefully. Because I
All operations are performed around the approximate number, which is equivalent to the operation on the number of its prime factor.
X = a1 ^ r1 * a2 ^ r2 *... * an ^ rn; set sum = r1 + r2 +... + rn.
Then all the situations can be expressed:
(1, sum-1), (2, sum-2 ),... (sum/2, sum-sum/2) or (1), (2 ),... (n-1)
This greatly reduces the data range. Then we can calculate the sum in this way.
Set a number to x and the smallest prime factor to y. Then sum [x] = sum [x/y] + 1;
The Code is as follows:
#include
#include
#include
#include using namespace std;const int maxn = 5000010;int prime[maxn],cnt;bool isprime[maxn];int fac_num[maxn];int min_fac[maxn];int sg[100];void GetPirme(){ cnt=0; memset(isprime,0,sizeof(isprime)); memset(fac_num,-1,sizeof(fac_num)); memset(min_fac,-1,sizeof(min_fac)); for(int i=2;i