Sicily 2611. Da Vinci Code

2611. Da Vinci Code

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

Welcome to the cyber world of the famous book “Da Vinci Code”! Before you start to explore the mystery inside, you should play a game with the author of the book, Dan Brown., to get entrance. Here are the general rules of the game:
       Before the game starts, Dan Brown will choose three positive integers Start, Target and N, then you and Dan Brown will take turns to select an integer from N candidate integers, and you play first. Initially the N candidate integers are Start, Start+1, …., Start+N-1, each time after an integer p is selected by a person, then the candidate integers that the other person can select from will become p+1, p+2, …, p+N. The one who selects an integer >= Target loses the game.
       As you and Dan Brown are both talents, each of you will follow the best strategy in the game. But you are not sure whether there is a winning strategy for you or not, and sometimes you even doubt that the game will definitely end with your loss (because the numbers Start, Target and N are chosen by Dan Brown!). That’s why now you are sitting in front of a computer and preparing to write a program to help you. Come on!

Input

Input may contain multiple test cases. The first line is a single integer T, (T<=100), the number of test cases below. Each test case consists of three integers, in the same line. These three integers are Start, Target and N as described above, (1<=Start<=10^100, 1<=Target<=10^100, 1<=N<=2^31-1).

Output

For each test case, if you cannot win, then simply output “NO” in a single line, otherwise, you should output the first integer that is selected by you and leads to your win. If this integer is not unique, you must output the minimum one.

Sample Input

2
1 3 2
3 3 1

Sample Output

2
NO

Problem Source

系列热身赛2@2011年上半学期算法考试和4+2选拔赛

// Problem#: 2611
// Submission#: 3593304
// 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>

const int MAXL = 111;

class HP {
public:
    HP() {
        len = 1;
        memset(data, 0, sizeof(data));
    }
    HP(int x) {
        len = 0;
        memset(data, 0, sizeof(data));
        while (x) {
            data[++len] = x % 10;
            x /= 10;
        }
        if (!len) len++;
    }
    HP(char *p) {
        len = 0;
        memset(data, 0, sizeof(data));
        while (p[len]) len++;
        for (int i = 1; i <= len; i++) data[i] = p[len - i] - '0';
    }
    bool smaller(const HP & that) {
        if (len < that.len) return true;
        if (len < that.len) return false;
        for (int i = len; i >= 1; i--) {
            if (data[i] < that.data[i]) return true;
            if (data[i] > that.data[i]) return false;
        }
        return false;
    }
    int modulo(int b) {
        long long ret = 0;
        for (int i = len; i >= 1; i--) ret = (ret * 10 + data[i]) % b;
        return (int)ret;
    }
    void printIn() {
        for (int i = len; i >= 1; i--) printf("%d", data[i]);
        printf("\n");
    }
    HP operator + (const HP & that) {
        HP ret;
        ret.len = len > that.len ? len : that.len;
        for (int i = 1; i <= ret.len; i++) {
            ret.data[i] += data[i] + that.data[i];
            if (ret.data[i] > 9) {
                ret.data[i] -= 10;
                ret.data[i + 1]++;
            }
        }
        if (ret.data[ret.len + 1]) ret.len++;
        return ret;
    }
    HP operator - (const HP & that) {
        HP ret;
        ret.len = len > that.len ? len : that.len;
        for (int i = 1; i <= ret.len; i++) {
            ret.data[i] += data[i] - that.data[i];
            if (ret.data[i] < 0) {
                ret.data[i] += 10;
                ret.data[i + 1]--;
            }
        }
        while (ret.len > 1 && !ret.data[ret.len]) ret.len--;
        return ret;
    }
private:
    int len;
    int data[MAXL];
};

int main() {
    int tn, n;
    char buf[MAXL];
    scanf("%d", &tn);
    while (tn--) {
        scanf("%s", buf);
        HP start(buf);
        scanf("%s", buf);
        HP end(buf);
        scanf("%d", &n);
        if (!start.smaller(end)) printf("NO\n");
        else {
            HP remain = end - start;
            int mod = remain.modulo(n + 1);
            if (!mod) printf("NO\n");
            else {
                HP ans = start + HP(mod - 1);
                ans.printIn();
            }
        }
    }
    return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值