An extension of the Nim game--51nod 1661 game on the blackboard +la 5059 Playing with stones__ number theory

Source: Internet
Author: User

A few days ago did a topic, is a NIM game expansion, also can not say expand it, just say another common state. Problem Introduction:

Given n heaps of stones, each stone has VI (1<=VI<=1E5), each can take a pile of some of the stones, so that the remaining stones for 1 to vi/k, for the final is the upper hand, or the next win, if the upper wins then output the corresponding operation strategy. thinking:

When the k==2, obviously degenerate into La 5059 's title, we take this as a basis to consider, we must first calculate the SG function table, and then observe the law, LRJ in the training guide is very clear, the rules of the table is also very simple, the following gives the table played by LRJ:
0 1 0 2 1 3 0 4 2 5
Of course, LRJ is analyzed from parity, so it's easy to draw
SG (x) =n%2==0. SG (N/2) This conclusion, because k==2, just have some features have been presented. So:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
ll SG (ll N)
{return
    n%2==0 N/2: SG (N/2);
}
int main ()
{
    int T;
    scanf ("%d", &t);
    while (t--)
    {
        int n;
        ll X;
        scanf ("%d", &n);
        ll Ans=0;
        while (n--)
        {
            scanf ("%lld", &x);
            ANS^=SG (x);
        }
        if (ans)
            printf ("yes\n");
        else
            printf ("no\n");
    }
    return 0;
}

So when the k>2, how to solve. Isn't Bishi. To launch
Sg= (n%k==0). N/K:SG (n/k) Such a conclusion, if true, I can only say that the ability to Bishi only in the answer, this ability should be a way to analyze the problem, the correct solution is nothing more than the table of the SG, looking for the law.

The author gave a push, then the table is as follows:

K==3:0 1 2 0 3 4 1 5 6
K==4:0 1 2 3 0 4 5 6 1 7 8 9

It can be found that there are 123456 ... This sequence, and then look at the n==2 of the above, found that there is such a law, but, as if no use, specific programming or no way to operate, and then to look at, found that interrupted 123456 the sequence of the subscript x is x%k==1, and remove 123456 of such numbers after the original sequence , then, that is, when x%k==1, the SG (x) =SG (x/k) Otherwise, this 123456 sequence is actually obtained by the X-step layer, what is the ladder layer, when the k==3 x=1,2,3 is the first layer, the x=4,5,6 belongs to the second layer, So easy to draw:

X%k==1? SG (x/k): x-x/k-(x%k!=0);

So, the second solution to the previous question is:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
ll k=2;
ll SG (ll x) {
   if (x==1) return 0;
   Return x%k==1? SG (x/k): x-x/k-(x%k!=0);
}
int main ()
{
    int T;
    scanf ("%d", &t);
    while (t--)
    {
        int n;
        ll X;
        scanf ("%d", &n);
        ll Ans=0;
        while (n--)
        {
            scanf ("%lld", &x);
            ANS^=SG (x);
        }
        if (ans)
            printf ("yes\n");
        else
            printf ("no\n");
    }
    return 0;
}

And for the 51nod requirements for the win operation, you need to preprocess the SG value, and then enumerate each bit according to the SG value to reverse the conditions of the X can.

Submit Address:
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1661

 #include <bits/stdc++.h> using namespace std;
typedef long Long LL;

const int n=1e5+5;
ll N,k,sl[n],sr[n],v[n],vt[n];
   ll SG (ll x) {if (x==1) return 0; Return x%k==1?
SG (x/k): x-x/k-(x%k!=0);
    int main () {sl[0]=sr[n+1]=0;
    Cin>>n>>k;ll p=0,ans=0;
        for (int i=1;i<=n;i++) {scanf ("%lld", v+i);
        VT[I]=SG (V[i]);
    Sl[i]=sl[i-1]^vt[i];
    for (int i=n;i>=1;i--) sr[i]=sr[i+1]^vt[i];
      for (int i=1;i<=n;i++) {ll tt=sl[i-1]^sr[i+1],tx=ceil (1.0*v[i]/k); if (v[i]==1| |
      VT[I]<=TT) continue;
      ll Ttl= (LL) (1.0*tt*k/(k-1) +1);
      if (ttl%k==1) ttl--;
          for (int j=0;j<64;j++) {if (TTL>=TX) break;
      ttl=ttl*k+1;
        } if (TTL<V[I]&&SG (TTL) ==tt) {p=i;ans=ttl;break;
    } if (Sl[n]) cout<< "Alice" <<p<< "" <<ans<<endl;
Else puts ("Bob"); }

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.