ACdream群賽1112(Alice and Bob)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   

題意:http://acdream.info/problem?pid=1112

Problem Description

Here  is Alice and Bob again !

Alice and Bob are playing a game. There are several numbers.First, Alice choose a number n.Then he can replace n (n > 1)with one of its positive factor but not itself or he can replace n with a and b.Here a*b = n and a > 1 and b > 1.For example, Alice can replace 6 with 2 or 3 or (2, 3).But he can’t replace 6 with 6 or (1, 6). But you can replace 6 with 1. After Alice’s turn, it’s Bob’s turn.Alice and Bob take turns to do so.Who can’t do any replace lose the game.

Alice and Bob are both clever enough. Who is the winner?

解法:非常好的博弈題。關鍵是找准每一個數的狀態。每一個數的狀態是每一個數的分解後的質數的個數。然後就是獲得每一個數的狀態號能夠做到On,就是線性篩素數時候順便將每一個數的最小的質數因子篩出來,從小大大先預先處理,然後求F(n)就等於F(n/least[n])+1。獲得狀態號,sg部分就不多說了。


代碼:

/******************************************************* author:xiefubao*******************************************************/#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <vector>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <string.h>//freopen ("in.txt" , "r" , stdin);using namespace std;#define eps 1e-8#define zero(_) (abs(_)<=eps)const double pi=acos(-1.0);typedef long long LL;const int Max=5000010;const int INF=1000000007;int f[300];bool rem[Max];int out[Max];int least[Max];int p=0;void init(){    for(int i=2; i<Max; i++)        if(!rem[i])        {            for(int j=i*2; j<Max; j+=i)            {                if(least[j]==-1)                    least[j]=i;                rem[j]=1;            }            least[i]=i;        }}int getans(int t){    if(f[t]!=-1)        return f[t];    int Rem[1000];    memset(Rem,0,sizeof Rem);    Rem[0]=1;    for(int i=1; i<=t/2; i++)    {        Rem[getans(i)]=1;        Rem[getans(t-i)]=1;        Rem[getans(t-i)^getans(i)]=1;    }    int to=0;    while(Rem[to])to++;    return f[t]=to;}int F(int n){    if(out[n]!=-1)        return out[n];    int ans=F(n/least[n])+1;    return out[n]=ans;}int main(){    int n;    memset(f,-1,sizeof f);    memset(out,-1,sizeof out);    memset(least,-1,sizeof least);    f[1]=1;    init();    out[1]=0;    for(int i=0; i<20; i++)        F(1<<i);// cout<<i<<" "<<getans(i)<<endl;    while(scanf("%d",&n)==1)    {        int ans=0;        for(int i=0; i<n; i++)        {            int t;            scanf("%d",&t);            ans^=getans(F(t));        }        if(ans)            puts("Alice");        else            puts("Bob");    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.