1. 程式人生 > >[HDU3544] Alice's Game(網上有些講解是錯的)

[HDU3544] Alice's Game(網上有些講解是錯的)

[HDU3544] Alice’s Game

題意:

 給n塊巧克力,第i塊是nimin_i*m_i,Alice只能垂直切,切成AmBmA*m和B*m,並且A+B=nA+B=n,Bob只能橫切,只能切成AnBnA*n和B*n,並且A+B=mA+B=m

分析:

語言表達能力有限

我們採取HP(n,m)HP(n,m)表示nmn*m的矩形對Alice的可切割次數的貢獻,負數代表對Bob的貢獻,如果所有HPi>0\sum{HP_i}> 0 ,Alice必贏,H

Pi<=0\sum{HP_i} <= 0,Bob(必贏)
對於HP(i,j) 的計算我們有如下法則
1. n1n*1 的矩形貢獻為n-1
2. 1m1*m 的矩形貢獻為-(m-1)
3. 22,33,44..nn2*2,3*3,4*4..n*n的矩形對HP的貢獻為零,因為如果你首先下手切,都會給對手更多的機會,如果你能贏,你不會切這個,如果你輸,那麼切了這個你還會輸。
4. 對於23,32,54...,2*3,3*2,5*4... ,的矩陣來說與3的狀況相同,對答案的貢獻都是0,首先下手都會給對手更多的機會
5. 貢獻為零的有什麼規律呢,我們發現原來是它們是 2
k<=n<2k+12^k <= n < 2^{k+1}
&&2k<=m<2k+12^k <= m < 2^{k+1}
6. n2n*2 的矩形對於Alice來說有貢獻,我們每一次可以選擇都切成22,322*2,3*2的矩形,這樣不會給對手機會,自己還可以增加一次切的機會,Bob也不會傻到切這個矩形,這樣會給Alice更多機會,所以n2n*2的矩形,Alice 可以切 n/2
1n/2-1

這個時候可以總結一下規律:

  1. 我們每一次切,都會切成HP(i,m)=0,HP(ni,m)>=0HP(i,m) = 0,HP(n-i,m) >= 0的 兩塊,HP(n,m)=HP(ni,m)+1HP(n,m) = HP(n-i,m)+1,遞迴下去HP(nij,m)=HP(nij,m)+1HP(n-i-j,m) = HP(n-i-j,m)+1,直到HP(nij....,m)=0HP(n-i-j....,m) = 0,這怎麼計算呢
  2. 我們知道HP(n,m)=0HP(n,m) = 0 當且僅當2k<=n<2k+12^k <= n < 2^{k+1}&&2k<=m<2k+12^k <= m < 2^{k+1}
  3. 那麼就有 HP(n,m)=n/(2k)1HP(n,m) = n/(2^{k}) - 1

在這裡插入圖片描述

參考程式碼

初級

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL solve(LL x,LL y)
{
    LL t  = 1;
    while(t*2 <= y)
        t <<= 1;
    return x/t;
}
int main()
{
    int T,n;
    scanf("%d",&T);
    for(int kase = 1; kase <= T; ++kase)
    {
        scanf("%d",&n);
        LL x,y;
        LL sum=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%lld%lld",&x,&y);
            if(x > y) sum += solve(x,y)-1;
            else sum -= solve(y,x) -1;
        }
        printf("Case %d: ",kase);
        if(sum > 0) puts("Alice");
        else        puts("Bob");
    }
    return 0;
}

簡化

下面這個版本就是大家在其他部落格上看到的答案了
就是不停的對兩邊除以2,其實原理和n/2k1n/2^{k}-1相同

#include <cstdio>
typedef long long LL;
int main()
{

    int T; scanf("%d", &T);
    for(int kase = 1; kase <= T; kase++)
    {
        int n; scanf("%d", &n);
        LL a = 0, b = 0;
        while(n--)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            while(x > 1 && y > 1) { x >>= 1; y >>= 1; }
            if(y == 1) a += (LL)x - 1;
            if(x == 1) b += (LL)y - 1;
        }
        printf("Case %d: %s\n", kase, a > b ? "Alice" : "Bob");
    }

    return 0;
}



相關推薦

[HDU3544] Alice's Game(網上有些講解的)

[HDU3544] Alice’s Game 題意:  給n塊巧克力,第i塊是ni∗min_i*m_ini​∗mi​,Alice只能垂直切,切成A∗m和B∗mA*m和B*mA∗m和B∗m,並且A+B=

[HDU3544] Alice's Game(網上有些講解的)

文章目錄 [HDU3544] Alice's Game 題意: 分析: 參考程式碼 初級 簡化 [HDU3544] Alice’s G

HDOJ 4944 FSF’s game

bmi mod cstring lld ++ main 20px tails training http://blog.csdn.net/keshuai19940722/article/details/38519681 不明真相的補一發。。。 FSF’s ga

hdu1525 Euclid&#39;s Game , 基礎博弈

post rac 題意 mod 基礎 pan pro game -s http://acm.hdu.edu.cn/showproblem.php?pid=1525 題意: 兩人博弈,給出兩個數a和b, 較大數減去較小數的隨意倍數。結果不能小於0,將兩個數隨

HDU - 2147 kiki's game

return to do hat won first appears perf art pear Recently kiki has nothing to do. While she is bored, an idea appears in his mind, she ju

HDU 1517: kiki's game

hat author have pro mil exti -1 until .cn /** * @link http://acm.hdu.edu.cn/showproblem.php?pid=1517 * @author Sycamore * @date Aug, 21 *

HDU 5643 King's Game | 約瑟夫環變形

printf for hdu bsp esp ret inf color long 經典約瑟夫環 1 int f[N] ={ 0 }; 2 for(int i=2; i<=n; i++) 3 { 4 f[i] = (f[i-1] + k) %

POJ 2348 Euclid's Game(輾轉相除博弈+自由度分析)

main -1 發現 轉移 pro b- color 思路 span 題目鏈接:http://poj.org/problem?id=2348 題目大意:給你兩個數a,b,Stan和Ollie輪流操作,每次可以將較大的數減去較小的數的整數倍,相減後結果不能小於0,誰先將其中

博弈論入門題 kiki's game

unsigned str ram right scan logs sel include otto Problem Description Recently kiki has nothing to do. While she is bored, an idea appear

【POJ】2348 Euclid's Game

情況 either play cond ive class 個人 lines always Description Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the

Codeforces 835E The penguin's game

quest rate 2個 lin c++ nat first ng- verdi Pay attention: this problem is interactive. Penguin Xoriy came up with a new game rece

HDU 2147 kiki's game

pac fec 棋盤 每次 contains ace sample rst include Problem Description Recently kiki has nothing to do. While she is bored, an idea appears i

hdu 5926.Mr. Frog’s Game

mmm ios problem color 上下 開心 include ostream 循環 http://acm.hdu.edu.cn/showproblem.php?pid=5926 題意:判斷給出的這個連連看能不能連上(一次就行) 水,在首行首列或者尾行尾列的需要判斷

Mr. Frog’s Game(模擬連連看)

nal ole -c amp sam sdi into pri day Description One day, Mr. Frog is playing Link Game (Lian Lian Kan in Chinese). In this game, if

POJ 2348 Euclid's Game(博弈)題解

tac pan tps 題意 \n sin typedef esp cstring 題意:有a,b兩個數字,兩人輪流操作,每次可以選擇兩個之中較小的數字,然後另一個數字減去選擇數字的任意倍數(不能減到負數),直到其中一個為0,不能操作為敗 思路:這題用博弈NP思想,必敗點和

UVa 10905 Children's Game

字串。。。直接排序(如果用字元陣列儲存不便於排序)   #include<cstdio> #include<cstring> #include<iostream> #include<cstdlib> #include<vec

o.s.b.d.LoggingFailureAnalysisReporter報

在寫springboot的時候,執行的時候出現o.s.b.d.LoggingFailureAnalysisReporter報錯 "C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" -Xdebug -Xnoagent -Djava.com

【2017ccpc final G - Alices Stamps HDU - 6249 】【dp】【01揹包變形】【取k個區間使得覆蓋範圍最大】

【連結】 acm.hdu.edu.cn/showproblem.php?pid=6249 【題意】 給你m個區間,要求你選出k個區間,使得區間並的覆蓋範圍最大 1≤T≤100 1≤K≤M 1≤N,M≤2000 1≤Li≤Ri≤N 【思路】 一開始我們得出錯誤的dp轉移:

HDU 3544Alice's Game 【不平等博弈)】

  Alice and Bob have got a lot of chocolates. All the chocolates are rectangles of different shapes as X i * Y i.They decide to

HDU1525 Euclid's Game 【歐幾里得博弈】

有兩個玩家,Stan 和 Ollie, 在玩遊戲。初始有兩個自然數。Stan是先手,每次把大的數字減去小的數字的任意倍數,但是不能使數字變成負數。然後Ollie進行同樣的操作,直到有一個玩家使一個數字變為零。 例如,初始時數字為(25,7): 25 7 11 7 4 7 4 3 1 3 1 0 這樣