Triple Nim(Nim博弈)

Time Limit: 2000 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

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.

 

Sample Input

3
3
6
14

Sample 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).

 

Source

“浪潮杯”山东省第七届ACM大学生程序设计竞赛

题目简述:

给出n个石子,分成三堆, 对手先行,每次从一堆中拿走至少一块石子,拿走最后一堆最后一块的人胜利,求对手不可能胜利的方案数。

题目简析:

3堆石子的Nim博弈。要对手必败,则要使对手拿石子时3堆石子个数二进制异或值为0。

当N为奇数时自己必输,因为无论怎么分3堆石子的个数都会有两个是偶数,异或值不可能为0.

当N为偶数时,要保证总异或值为0,则每个二进制位要有两个1,所以要把给定的石头数N化成二进制,把每一位的1拆成另外两堆(把石子分成三堆,目前操作的假设为第一堆,其他两个分别为第二,第三堆)低一位的两个1,结束后二进制位全为0。然后对零进行排列,有3^num(num为把N化成二进制后1的个数)种排法,因为每一堆石子个数都不能为零所以要减3,得有3^num-3种排法,因为排列有重复如(1,2,3)和(3,2,1)为1种分法,所以要除6。

#include<cstdio>
#include<iostream>
#include<math.h>
typedef long long ll;

using namespace std;

int main()
{
    int t,num;
    ll  ans;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        if(n%2)  //奇数
            printf("0\n");
        else{
            num = 0;
            while(n){
                if(n&1)
                    num++;
                n>>=1;
            }
            ans = ((ll)pow(3,num)-3)/6;
            printf("%lld\n",ans);
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值