ACdream 1112 Alice and Bob (game & amp; prime screening optimization)

Source: Internet
Author: User

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
    
     

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.