Ultraviolet A 12009-avaricious maryanna (violent)

Source: Internet
Author: User

Connection: Ultraviolet A 12009-avaricious maryanna

Given n, x and X are n digits, and the last n digits of x * X are still X.

Solution: When you create a table, you will find that there are actually regular rules. Except when n = 1, there are 0 and 1 more. The other values are n-1 digits, and then a new number of digits is added, the first digit is 5, 6.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 500;int a[maxn+5], b[maxn+5];int mul (int* p, int n) {    int q[maxn+5];    memset(q, 0, sizeof(q));    for (int i = 1; i < n; i++) {        int t = 0, flag = true;        for (int j = 1; j < n; j++) {            if (i + j - 1 > n) {                flag = false;                break;            }            t += p[i] * p[j] + q[i+j-1];            q[i+j-1] = t % 10;            t /= 10;        }        if (flag) {            int mv = i+n-1;            while (t) {                q[mv++] = t % 10;                t /= 10;            }        }    }    /*       for (int i = 1; q[i]; i++)       printf("%d", q[i]);       printf("\n");       */    return q[n];}void init () {    a[1] = 5;    b[1] = 6;    mul(a, 2);    for (int i = 2; i <= maxn; i++) {        int ra = 0, rb = 0;        int p = mul(a, i);        int q = mul(b, i);        for (int k = 0; k <= 9; k++) {            if ((2 * k * a[1] + p) % 10 == k)                ra = k;            if ((2 * k * b[1] + q) % 10 == k)                rb = k;        }        a[i] = ra;        b[i] = rb;    }}void put (int* num, int n) {    printf(" ");    for (int i = n; i; i--)        printf("%d", num[i]);}int main () {    init();    int cas, n;    scanf("%d", &cas);    for (int k = 1; k <= cas; k++) {        scanf("%d", &n);        printf("Case #%d:", k);        if (n == 1)            printf(" 0 1");        if (a[n] && b[n]) {            if (a[n] > b[n]) {                put(b, n);                put(a, n);            } else {                put(a, n);                put(b, n);            }        } else if (a[n]) {            put(a, n);        } else if (b[n])            put(b, n);        printf("\n");    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.