NC24877. [USACO 2009 Ope S]Cow Digit Game
描述
输入描述
* Line 1: A single integer: G
* Lines 2..G+1: Line i+1 contains the single integer: Ni
输出描述
* Lines 1..G: Line i contains 'YES' if Bessie can win game i, and 'NO' otherwise.
示例1
输入:
2 9 10
输出:
YES NO
说明:
For the first game, Bessie simply takes the number 9 and wins. For the second game, Bessie must take 1 (since she cannot take 0), and then FJ can win by taking 9.C++14(g++5.4) 解法, 执行用时: 33ms, 内存消耗: 4192K, 提交时间: 2019-08-05 16:08:05
#include <stdio.h> #include <algorithm> using namespace std; const int N=1e6+10; int sg[N]; int main(){ for(int i=1;i<=1e6;i++){ int ma=0,mi=9,t=i; while(t){ int b=t%10; if(b){ ma=max(b,ma); mi=min(b,mi);} t/=10; } sg[i]=(sg[i-ma]&sg[i-mi])^1; } int t; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); printf("%s",sg[n]?"YES\n":"NO\n"); } return 0;}
C++11(clang++ 3.9) 解法, 执行用时: 32ms, 内存消耗: 4444K, 提交时间: 2019-08-12 11:18:17
#include<bits/stdc++.h> using namespace std; const int N=1e6+10; int sg[N]; int main() { for(int i=1;i<=1e6;i++){ int ma=0,mi=9,t=i; while(t){ int b=t%10; if(b){ ma=ma>b?ma:b; mi=mi<b?mi:b; } t/=10; } sg[i]=(sg[i-ma]&sg[i-mi])^1; } int t; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); printf("%s",sg[n]?"YES\n":"NO\n"); } return 0; }