您的位置:首页 > 其它

山东省第七届ACM省赛------Triple Nim

2016-06-12 21:26 411 查看

Triple Nim

Time Limit: 2000MS Memory limit: 65536K

题目描述

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.

输入

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.

输出

One line per case. The number of ways Bob can make Alice never win.

示例输入

3

3

6

14


示例输出

0

1

4


提示

In the third case, Bob can make three heaps (1,6,7), (2,5,7), (3,4,7) or (3,5,6).

来源

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

题意

应该是一道博弈的题目吧!因为自己没有看过博弈这方面的知识,听学长说,这道题目的意思就是给你一个数,把它拆分成三个数的和,并且这三个数异或值等于0,都是博弈的知识啦~
既然找三个数,并且这三个数的异或等于0,那么就要保证三个数的二进制对应位每一列必须有两个1,或者全是0.
比如14可以分解成
1110
1110
0000
如果给的数是奇数的话,它分解成的二进制最后一位的和必须是1,因此只能出现最后一位111或者001的情况了,所以奇数一定不可能有这样的组合数,直接输出0.
若是偶数,它拆分之后的组合与本身二进制中1的个数有关。
14二进制中有三个1,而每一个1都是它分解成三个二进制数次位两个1相加进位得到的,也就是说,14这个数二进制有三个1,组合的情况便是3^3,减去三个数中某个数单独为0的情况3^3-3,又由于每种排列内部排序会出现A33这样的情况,所以要除去它
最终得出公式
ans=(3^m-3)/6 m为二进制中1的个数
这样便大功告成啦~,为了快捷,我们也可以对它进行打表

AC代码:
#include"stdio.h"
#include"string.h"
#include<iostream>
using namespace std;
long long s1(long long int n)   //求一个数二进制1的个数
{
long long s=0;
while(n>>=1)
if(n&1)++s;
return s-1;
}
long long f[30]= {1};
void init()                     //打表
{
for(int i=1; i<30; i++)
f[i]=f[i-1]*3+1;
}
int main()
{
int N;
init();
cin>>N;
while(N--)
{
long long int n;
cin>>n;
if(n&1)printf("0\n");
else
{
long long int dd=s1(n)-1;
cout<<f[dd]<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: