「HNOI 2014」 江南乐

(Description)

(n)堆石子,每堆石子有(s_i)个,两个人轮流操作,每次可以将一对不少于(F)的石子尽量平均分成(m)堆,(m)每次自选,不能操作者输.共有(T)组数据

(Solution)

(70 pts)

直接(SG)搞一搞就好了,枚举堆的个数,异或一下就没了

(Code)

#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const int mod=1e9+7;
int read(){
    int x=0,f=1;char c=getchar();
    while(c<'0'||c>'9') f=(c=='-')?-1:1,c=getchar();
    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}
int sg[100010],mex[100010],f,T;
int SG(int x){
    if(sg[x]!=-1) return sg[x];
    if(x<f) return sg[x]=0;
    sg[x]=0;
    for(int i=2;i<=x;i++){
        int res=0,p=i-x%i,pp=x%i,c;
        if(pp&1) c=SG(x/i+1),res^=c;
        if(p&1) c=SG(x/i),res^=c;
        mex[res]=x;
    }
    while(mex[sg[x]]==x)
        sg[x]++;
    return sg[x];
}
main(){
    T=read(),f=read();
    memset(sg,-1,sizeof(sg));
    while(T--){
        int n=read(),ans=0,x;
        for(int i=1;i<=n;i++)
            x=read(),ans^=SG(x);
        printf("%d ",ans?1:0);
    }
}

(100 pts)

假设现在求的是(x)(sg)值,我们动笔算一算,发现他每次求的都是:

[lfloor frac{x}{i} floor,lfloor frac{x}{i+1} floor,lfloor frac{x}{i+2} floor,lfloor frac{x}{i+3} floor... ]

但是这里面会有很多相等的答案,这个学过整除分块的应该都知道吧.

如果没学过就去看一看,很好理解.

所以对于每一个相同的答案只要计算(i)(i+1)就好了

(Code)

#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const int mod=1e9+7;
int read(){
    int x=0,f=1;char c=getchar();
    while(c<'0'||c>'9') f=(c=='-')?-1:1,c=getchar();
    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}
int sg[100010],mex[100010],f,T;
int SG(int x){
    if(sg[x]!=-1) return sg[x];
    if(x<f) return sg[x]=0;
    sg[x]=0;
    for(int j=2;j<=x;j=x/(x/j)+1)
    for(int i=j;i<=min(j+1,x);i++){
        int res=0,p=i-x%i,pp=x%i;
        if(pp&1) res^=SG(x/i+1);
        if(p&1) res^=SG(x/i);
        mex[res]=x;
    }
    while(mex[sg[x]]==x)
    sg[x]++;
    return sg[x];
}
main(){
    T=read(),f=read();
    memset(sg,-1,sizeof(sg));
    while(T--){
    int n=read(),ans=0,x;
    for(int i=1;i<=n;i++)
        x=read(),ans^=SG(x);
    printf("%d ",ans?1:0);
    }
}
原文地址:https://www.cnblogs.com/hbxblog/p/10422829.html