Codeforces Round #114 (Div. 1) C Wizards and Numbers

题目链接:Wizards and Numbers

题意:有两个数字a, b,假设 a>b,两个玩家进行游戏,有两种操作:1. 令 a=a-b^k,k为正数,且需要保证操作后 a 仍然非负;2. 令 a=a%b。最终有一个人无法操作(存在一个数为0)的时候便输了。

题解:如果只有操作2就是一个辗转相除法,实际上加上了操作1,执行的过程中也一定会出现辗转相除法种每轮的数字情况。所以以辗转相除法的每轮情况作为分段点,讨论其是必胜态还是必败态,首先(t, 0) 一定是必败态,所以其上一层的状态一定是必胜态。主要考虑当前层为必胜态,那上一层的情况为什么,问题实际上可以转化为有 tx 堆石子,每次可以 x的幕次颗,谁最先取光谁就输了。打个表找找规律便可以了:若 x 为奇数,则结果与 t 的奇偶有关;若 x 为偶数,则会在 x^2 处作为分界点,规律发生了变化,但还是很显然的。

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

LL xx[105],yy[105],cnt;

LL gcd(LL x,LL y){
    int res=(y==0?x:gcd(y,x%y));
    xx[++cnt]=x;
    yy[cnt]=y;
    return res;
}

LL ans[105];
bool check(LL a,LL b){
    if(a%2==1){
        return (b/a-1)%2;
    }
    else{
        if(a>=1000000000||b<a*a){
            return !((b/a)%2);
        }
        else{
            LL res=b/a;
            LL c1=(res%(a+1))%2;
            LL c2=res%(a+1);
            return !c1||(c2==a);
        }
    }
}
void solve(LL a,LL b){
    cnt=0;
    gcd(a,b);
    ans[1]=0;
    for(int i=2;i<=cnt;i++){
        if(ans[i-1]==0) ans[i]=1;
        else if(check(yy[i],xx[i]-yy[i-1])) ans[i]=1;
        else ans[i]=0;
    }
    if(ans[cnt]) printf("First\n");
    else printf("Second\n");
}

int p=12;
int biao[100005];
int dabiao(int x){
    if(biao[x]!=-1) return biao[x];
    if(x<0) return 0;
    int temp=p;
    int flag=0;
    while(temp<=x*p){
        if(dabiao(x-temp/p)==0){
            flag=1; break;
        }
        temp*=p;
    }
    return biao[x]=flag;
}

int main(){
    /*******
    memset(biao,-1,sizeof(biao));
    biao[0]=1;
    dabiao(100);
    for(int i=1;i<=100;i++){
        cout<<i<<" "<<biao[i]<<endl;
    }
    *******/
    int n;
    cin>>n;
    while(n--){
        LL a,b;
        cin>>a>>b;
        if(a<b) swap(a,b);
        if(b==0) cout<<"Second"<<endl;
        else if(a%b==0) cout<<"First"<<endl;
        else solve(a,b);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/N-Psong/p/10281699.html

时间: 2024-07-30 16:56:38

Codeforces Round #114 (Div. 1) C Wizards and Numbers的相关文章

Codeforces Round #597 (Div. 2) A. Good ol&#39; Numbers Coloring

链接: https://codeforces.com/contest/1245/problem/A 题意: Consider the set of all nonnegative integers: 0,1,2,-. Given two integers a and b (1≤a,b≤104). We paint all the numbers in increasing number first we paint 0, then we paint 1, then 2 and so on. Ea

Codeforces Round #257 (Div. 1) D - Jzzhu and Numbers 容斥原理 + SOS dp

D - Jzzhu and Numbers 这个容斥没想出来... 我好菜啊.. f[ S ] 表示若干个数 & 的值 & S == S得 方案数, 然后用这个去容斥. 求f[ S ] 需要用SOSdp #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define

Codeforces Round #327 (Div. 2)-Wizards&#39; Duel

题意: 在一条长度为l的走廊,两个人站在走廊的左右两端分别以p,q的速度走来,问他们相遇时离左端的距离是多少? 思路: 非常简单的暴力题,不解释. 代码如下: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <fstream> 5 #include <ctime> 6 #include <cmath> 7 #include <cs

Codeforces Round #259 (Div. 2) 解题报告

终于重上DIV1了.... A:在正方形中输出一个菱形 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月01日 星期五 23时27分55秒 4 5 #include<vector> 6 #include<set> 7 #include<deque> 8 #include<stack> 9 #include<bitset> 10 #inclu

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #424 (Div. 2) E. Cards Sorting(线段树)

题目链接:Codeforces Round #424 (Div. 2) E. Cards Sorting 题意: 将n个数放进一个队列,每次检查队首,看看是不是队列中最小的数,如果是就扔掉,如果不是就放到队尾. 这样直到队列为空,为需要操作多少次. 题解: 考虑用两个指针模拟,最开始now指针指向第一个数,然后nxt指针指向下一个将要被删除的数. 然后我们要算出这里需要移动多少步,然后删掉这个数,一直重复操作,直到将全部的数删完. nxt指针可以用set来维护,now指针可以用并查集来维护. 计

Codeforces Round #366 (Div. 2) ABC

Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 1 #I hate that I love that I hate it 2 n = int(raw_input()) 3 s = "" 4 a = ["I hate that ","I love that ", "I hate it","I love it"] 5 fo

二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

题目传送门 1 /* 2 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 9 const int MAXN = 5e2 + 10; 10 const int MAXM = 1e6 + 10; 11 const int INF = 0