Sicily 1305. Who’s Winner?

1305. Who’s Winner?

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Nic and Susan play the game of multiplication by multiplying an integer p by one of the numbers 2 to 9. Nic
always starts with p=1, does his multiplication. Then Susan multiplies the number, then Nic and so on. Before a game starts, they draw an integer 1≤n<4,294,967,295 and the winner is who first reaches p≥n.

Input

Each line of input contains one integer number n.

Output

For each line of input output one line either

Nic wins.

or

Susan wins.

Assume that both of them play perfectly.

Sample Input

162
17
34012226

Sample Output

Nic wins.
Susan wins.
Nic wins.

Problem Source

ZSUACM Team Member

这题在编程书上看到了好高深的解法。。0.03s:

// Problem#: 1305
// Submission#: 2777051
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <string.h>

long long power2[33], power3[22], power5[15], power7[13];

void make_power() {
    int i;
    for (power2[0] = 1, i = 1; i < 33; power2[i] = 2 * power2[i - 1], i++);
    for (power3[0] = 1, i = 1; i < 22; power3[i] = 3 * power3[i - 1], i++);
    for (power5[0] = 1, i = 1; i < 15; power5[i] = 5 * power5[i - 1], i++);
    for (power7[0] = 1, i = 1; i < 13; power7[i] = 7 * power7[i - 1], i++);
}

bool a[34][23][16][14];  //判断输赢

//下面的循环是设立一个待定者,这个待定者在大于等于target的时候是输的,那么由此推出只要在使得数字大于等于target的上一步就是赢的,然后由此往前推,推到第一个位置

int main() {

    make_power();

    long long target;
    while (~scanf("%lld", &target)) {

        if (target == 1) {
            printf("Nic wins.\n");
            continue;
        }

        memset(a, false, sizeof(a));

        int p2, p3, p5, p7;
        for (p2 = 32; p2 >= 0; p2--) {
            for (p3 = 21; p3 >= 0; p3--) {
                for (p5 = 14; p5 >= 0; p5--) {
                    for (p7 = 12; p7 >= 0; p7--) {

                        if (power2[p2] * power3[p3] * power5[p5] * power7[p7] < target) {

                            if (p2 < 32 && a[p2 + 1][p3][p5][p7] == false) { //乘2之后是输的,那么这一步就是赢的
                                a[p2][p3][p5][p7] = true;
                                continue;
                            }

                            if (p3 < 21 && a[p2][p3 + 1][p5][p7] == false) {
                                a[p2][p3][p5][p7] = true;
                                continue;
                            }

                            if (p2 < 31 && a[p2 + 2][p3][p5][p7] == false) {
                                a[p2][p3][p5][p7] = true;
                                continue;
                            }

                            if (p5 < 14 && a[p2][p3][p5 + 1][p7] == false) {
                                a[p2][p3][p5][p7] = true;
                                continue;
                            }

                            if (p2 < 32 && p3 < 21 && a[p2 + 1][p3 + 1][p5][p7] == false) {
                                a[p2][p3][p5][p7] = true;
                                continue;
                            }

                            if (p7 < 12 && a[p2][p3][p5][p7 + 1] == false) {
                                a[p2][p3][p5][p7] = true;
                                continue;
                            }

                            if (p2 < 30 && a[p2 + 3][p3][p5][p7] == false) {
                                a[p2][p3][p5][p7] = true;
                                continue;
                            }

                            if (p3 < 20 && a[p2][p3 + 2][p5][p7] == false) {
                                a[p2][p3][p5][p7] = true;
                                continue;
                            }
                        }
                    }
                }
            }
        }

        if (a[0][0][0][0]) {
            printf("Nic wins.\n");
        } else {
            printf("Susan wins.\n");
        }
    }
    return 0;
}                             

结果找规律:

假如Susan赢,那么Nic肯定要尽量拖慢她,就尽量乘2,相反,假如Nic赢,Nic肯定尽量乘9;

赢的人:

1 N

2~9 (1 * 9) N

10~18 (1 * 9 * 2) S

19~162 (1 * 9 * 2 * 9) N

163~324 (1 * 9 * 2 * 9 * 2) S

说明乘二之后大于等于目标的就是S赢,乘9后大于等于目标的N赢:0s:

#include <stdio.h>

int main() {
    long long n;
    while (~scanf("%lld", &n)) {
        if (n == 1) {
            printf("Nic wins.\n");
            continue;
        }
        long long temp = 1;
        while (1) {
            temp *= 9;
            if (temp >= n) {
                printf("Nic wins.\n");
                break;
            }
            temp *= 2;
            if (temp >= n) {
                printf("Susan wins.\n");
                break;
            }
        }
    }
    return 0;
}

时间: 2024-10-09 22:02:17

Sicily 1305. Who’s Winner?的相关文章

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

(转)sicily题目分类

Sicily题目分类 ·         [数据结构/图论] 1310 Right-Heavy Tree   笛卡尔树相关,复杂度O(N)或O(NlogN). ·1426 Phone List         电话号码前缀检索,trie树相关. ·1443 Printer Queue      基本队列操作. ·1149 等价表达式         判断表达式是否等价(递归求解) ·1136 山海经             n长序列里求m次区间询问的最大连续子区间和.线段树/RMQ ·1252

CodeForces-2015 HIAST Collegiate Programming Contest-Gym-100952A-Who is the winner?

A. Who is the winner? time limit per test 1 second memory limit per test 64 megabytes input standard input output standard output A big marathon is held on Al-Maza Road, Damascus. Runners came from all over the world to run all the way along the road

Sicily 1146:Lenny&#39;s Lucky Lotto(dp)

题意:给出N,M,问有多少个长度为N的整数序列,满足所有数都在[1,M]内,并且每一个数至少是前一个数的两倍.例如给出N=4, M=10, 则有4个长度为4的整数序列满足条件: [1, 2, 4, 8], [1, 2, 4, 9], [1, 2, 4, 10], [1, 2, 5, 10] 分析:可用动态规划解题,假设dp[i][j],代表满足以整数i为尾数,长度为j的序列的个数(其中每一个数至少是前一个数的两倍).那么对于整数i,dp[i][j] 等于所有dp[k][j-1]的和,其中k满足:

HDU2509 Be the Winner

Be the Winner Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3062    Accepted Submission(s): 1709 Problem Description Let's consider m apples divided into n groups. Each group contains no more

HDU 5754 Life Winner Bo 组合博弈

Life Winner Bo Problem Description Bo is a "Life Winner".He likes playing chessboard games with his girlfriend G. The size of the chessboard is N×M.The top left corner is numbered(1,1) and the lower right corner is numberd (N,M). For each game,B

Diabetic Retinopathy Winner&#39;s Interview: 1st place, Ben Graham

Diabetic Retinopathy Winner's Interview: 1st place, Ben Graham Ben Graham finished at the top of the leaderboard in the high-profileDiabetic Retinopathy competition. In this blog, he shares his approach on a high-level with key takeaways. Ben finishe

Facebook IV Winner&#39;s Interview: 1st place, Peter Best (aka fakeplastictrees)

Facebook IV Winner's Interview: 1st place, Peter Best (aka fakeplastictrees) Peter Best (aka fakeplastictrees) took 1st place in Human or Robot?, our fourth Facebook recruiting competition. Finishing ahead of 984 other data scientists, Peter ignored

sicily 1345 能量项链

先模拟一下确定理解题意,然后再找状态转移方程,注意方向~ 1 //sicily 1345 能量项链 2 #include <bits/stdc++.h> 3 4 using namespace std; 5 6 int a[205]; 7 int dp[205][205]; 8 9 int main() 10 { 11 int n; 12 while(cin >> n) 13 { 14 memset(dp, 0, sizeof(dp)); 15 for(int i=0; i<