Sicily 2611. Da Vinci Code

来源:互联网 发布:视频剪切软件绿色版 编辑:程序博客网 时间:2024/05/22 03:39

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

21 3 23 3 1

Sample Output

2NO

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;}                                 


0 0