codeforces662a(线性基)

A. Gambling Nim

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

As you know, the game of "Nim" is played with n piles of stones, where the i-th pile initially contains ai stones. Two players alternate the turns. During a turn a player picks any non-empty pile and removes any positive number of stones from it. The one who is not able to make a move loses the game.

Petya and Vasya are tired of playing Nim, so they invented their own version of the game and named it the "Gambling Nim". They have ntwo-sided cards, one side of the i-th card has number ai written on it, while the other side has number bi. At the beginning of the game the players put all the cards on the table, each card only one of its sides up, and this side is chosen independently and uniformly. Thus they obtain a sequence c1, c2, ..., cn, where ci is equal to ai or bi. Then they take n piles of stones, with i-th pile containing exactly cistones and play Nim. Petya takes the first turn.

Given that both players play optimally, find the probability of Petya's victory. Output the answer as an irreducible fraction.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) — the number of cards in the deck.

Each of the following n lines contains the description of one card, consisting of two integers ai and bi (0 ≤ ai, bi ≤ 1018).

Output

Output the answer as an irreducible fraction p / q. If the probability of Petya's victory is 0, print 0/1.

Examples

input

Copy

2
1 1
1 1

output

Copy

0/1

input

Copy

2
1 2
1 2

output

Copy

1/2

input

Copy

3
0 4
1 5
2 3

output

Copy

1/1

题意:给出一组牌(有n张),每张牌都有a、b两面,都有数字,现在每张牌a、b两面随机放置,朝上或为ai或为bi,以朝上的数字为准,进行Nim博弈,求得最后先手获胜几率。

思路:线性基啊!我之前学过的啊!!https://blog.csdn.net/qq_25576697/article/details/83005007

可是太久没碰了,忘了。

假设牌面朝上全为a,那么异或起来设为s,设ai^bi=ci,现在如果要求先手必败,则转换为b的那几张牌,要求ci异或起来等于s,只有这样才能达到朝上的异或起来等于0,这个时候就用到线性基。如果s不能用ci线性基表示,那么先手必胜1/1.否则,设线性基大小为cnt,则s的表示方法种数为2^(n-cnt),所以先手必败=(2^(n-cnt)/2^n) = 1/2^cnt,先手必胜=(2^cnt-1/2^cnt)。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5+100;
ll c[maxn];
ll p[62];
int cnt;
int add(ll x)
{
    for (ll i=62;i>=0;--i)
        if (x>>i&1)
        {
        if (!p[i])
        {
            cnt++;
            p[i]=x; 
            return 0; 
        }
        x^=p[i];
    }
    return 1;
}
int main()
{
    memset(p,0,sizeof(p));
    cnt = 0;
    int n;
    scanf("%d",&n);
    ll a,b;
    ll s = 0;
    for(int i = 0;i<n;i++)
    {
        scanf("%I64d%I64d",&a,&b);
        s ^= a;
        c[i] = a^b;
        add(a^b);
    }
    if(add(s))
        printf("%I64d/%I64d\n",((1ll<<cnt)-1),(1ll<<cnt));
    else
        printf("1/1\n");
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值