码迷,mamicode.com
首页 > 其他好文 > 详细

uva 12009 - Avaricious Maryanna(暴力)

时间:2017-04-16 13:10:41      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:cat   clu   href   algo   round   思路   cio   string   pac   

题目连接:uva 12009 - Avaricious Maryanna

题目大意。给定n。求x。x为n位数,而且x*x的后n位还是x。

解题思路:打个表会发现事实上有规律,除了n=1的时候多了0和1。其它都是n-1位的基础上再新增一位数,1位的时候是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;
}

uva 12009 - Avaricious Maryanna(暴力)

标签:cat   clu   href   algo   round   思路   cio   string   pac   

原文地址:http://www.cnblogs.com/zsychanpin/p/6718425.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!