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

uva 11256 - Repetitive Multiple(gcd+暴力)

时间:2014-08-03 12:54:35      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   io   for   ar   line   

题目链接:uva 11256 - Repetitive Multiple

题目大意:给定一个数n,要求找到最小的k,使得k?n为题目中定义的重复数字.

解题思路:枚举k?n的循环节长度,比如当前枚举为2,那么一次判断u=1001,1001001,1001001001 ...,取d = gcd(n,u), 那么k = u / d, a = n / d (因为n?k=u?a)并且保证a的长度为2,所以k和a要同时扩大相应倍数。枚举过程中为何k。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;

const ll INF = 1e18+1;
ll N, ten[20];

void init () {
    ten[0] = 1;
    for (int i = 1; i < 20; i++)
        ten[i] = ten[i-1] * 10;
}

inline ll gcd (ll a, ll b) {
    return b == 0 ? a : gcd(b, a%b);
}

inline int tenbit (ll n) {
    int cnt = 0;
    while (n) {
        n /= 10;
        cnt++;
    }
    return cnt;
}

ll solve () {
    ll ans = INF;
    int bit = tenbit(N);

    if (bit == 1)
        return 11L;

    for (int i = 1; i <= bit; i++) {
        ll u = ten[i] + 1;

        while (true) {
            ll d = gcd(N, u);

            ll a = N / d;
            ll k = u / d;


            if (a < ten[i]) {

                if (a < ten[i-1])
                    a = ten[i-1] / a + (ten[i-1] % a ? 1 : 0);
                else
                    a = 1;

                if (a * k < ans)
                    ans = k * a;
            }

            if (INF / u < ten[i])
                break;

            u = u * ten[i] + 1;
        }
    }
    return ans;
}

int main () {
    init();

    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%lld", &N);
        while (N >= 1e9);
        printf("%lld\n", solve() * N);
    }
    return 0;
}

uva 11256 - Repetitive Multiple(gcd+暴力),布布扣,bubuko.com

uva 11256 - Repetitive Multiple(gcd+暴力)

标签:style   http   color   os   io   for   ar   line   

原文地址:http://blog.csdn.net/keshuai19940722/article/details/38353199

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