第7届山东省赛sdut 3566---Triple Nim


Problem Description

Alice and Bob are always playing all kinds of Nim games and Alice always goes first. Here is the rule of Nim game:

    There are some distinct heaps of stones. On each turn, two players should remove at least one stone from just one heap. Two player will remove stone one after another. The player who remove the last stone of the last heap will win.

    Alice always wins and Bob is very unhappy. So he decides to make a game which Alice will never win. He begins a game called “Triple Nim”, which is the Nim game with three heaps of stones. He’s good at Nim game but bad as math. With exactly N stones, how many ways can he finish his target? Both Alice and Bob will play optimally.

Input
 Multiple test cases. The first line contains an integer T (T <= 100000), indicating the number of test case. Each case contains one line, an integer N (3 <= N <= 1000000000) indicating the number of stones Bob have.
Output
 One line per case. The number of ways Bob can make Alice never win.
Example Input
3
3
6
14
Example Output
0
1
4
Hint
 In the third case, Bob can make three heaps (1,6,7), (2,5,7), (3,4,7) or (3,5,6).

题意:传统的Nim游戏,一共有3堆,给出n个石头,令先拿者输的策略有哪几种石头的分布

省赛的题目,比赛的时候没有做出来,赛后分析题解明白。

就是把n个石头分为3部分,使得数量异或为0的种数。由于是3堆异或,由异或的性质,只有对应的每个数位1^1^0=0才可。

1.当n为奇数时,必赢。因为此时必定是2个偶数堆,1个奇数堆,异或时最后一位是0^0^1=1,所以不可能是异或为0,输出0种。

2.当n为偶数时,分析30个石子即11110的情况。此时最高位的1必定要分解为低一位的2个1,每个数位要保持2个1和1个0的情况,所以依次分解。

   2个1 2个1 2个1

0    _     _     _

1    _     _     _

1    _     _     _

此时的种数为(3^3-1)/2,其中-1是0 0 0 0的情况,此时数量为0不可,/2是下面两列的情况1种算了2种,比如(1,3,4)(1,4,3)


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        ll n;
        cin>>n;
        if(n&1)
            cout<<0<<endl;
        else
        {
            int num=__builtin_popcount(n);//计算n的二进制数中的1的个数
            cout<<(ll)(pow(3,num-1)-1)/2<<endl;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值