UVA 11256 Repetitive Multiple (整数循环节)

Description:

An integer is said to be repetitive if it can be written as a concatenation of several copies (at least two) of another non-zero-leading integer. For example, 11, 123123, 454545 are all repetitive integers. Since zero-leading integers are not allowed, 101 can not be considered as 0101. Therefore, 101 is not repetitive.
Given a positive integer n, what is the smallest repetitive integer which is a multiple of n.

Input

The input begins with an integer N (≤ 100) which indicates the number of test cases followed. Each of the following test cases consists of a positive integer n, where n will be less than 109 .

Output

For each test case, print out the smallest repetitive multiple of n in a single line.

Sample Input

5
7
101
123
999999
6339673

Sample Output

77
1010
33333
999999
114114114

题意:

给一个 n n n,问 n n n 的倍数中最小的循环整数是多少。循环整数的定义是一个没有前导 0 0 0 的整数有某一部分重复 x x x 次构成。

比如 11 , 123123 , 454454 11,123123,454454 11123123,454454,像 101 101 101 不能写成 0101 0101 0101 而被看成循环整数。

对于一个长度为 q q q 的整数,循环节长度肯定是 q q q 的因子,比如长度为 6 6 6 的整数,循环节可以是 1 , 2 , 3 1,2,3 1,2,3,不能是它本身,如果循环节长度是 3 3 3 的,必然可以写成 1001 ∗ x 1001*x 1001x ,其中 99 < x < 1000 99 < x < 1000 99<x<1000,循环节长度为 2 2 2 的可以写成 10101 ∗ x 10101*x 10101x,其中 9 < x < 100. 9 < x < 100. 9<x<100.
其实原理就是循环节长度为 3 3 3 那么就是 1 ∗ 一 个 三 位 数 + 1000 ∗ 一 个 三 位 数 1*一个三位数+1000*一个三位数 1+1000 这样正好可以组成一个循环的。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sc(n) scanf("%c", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define ss(str) scanf("%s", str)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret * sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
    if (a > 9)
        Out(a / 10);
    putchar(a % 10 + '0');
}

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

ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}
///快速幂m^k%mod
ll qpow(ll a, ll b, ll mod)
{
    if (a >= mod)
        a = a % mod + mod;
    ll ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = ans * a;
            if (ans >= mod)
                ans = ans % mod + mod;
        }
        a *= a;
        if (a >= mod)
            a = a % mod + mod;
        b >>= 1;
    }
    return ans;
}

// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
    return qpow(a, p - 2, p);
}

///扩展欧几里得
int exgcd(int a, int b, int &x, int &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    int g = exgcd(b, a % b, x, y);
    int t = x;
    x = y;
    y = t - a / b * y;
    return g;
}

///使用ecgcd求a的逆元x
int mod_reverse(int a, int p)
{
    int d, x, y;
    d = exgcd(a, p, x, y);
    if (d == 1)
        return (x % p + p) % p;
    else
        return -1;
}

///中国剩余定理模板0
ll china(int a[], int b[], int n) //a[]为除数,b[]为余数
{
    int M = 1, y, x = 0;
    for (int i = 0; i < n; ++i) //算出它们累乘的结果
        M *= a[i];
    for (int i = 0; i < n; ++i)
    {
        int w = M / a[i];
        int tx = 0;
        int t = exgcd(w, a[i], tx, y); //计算逆元
        x = (x + w * (b[i] / t) * x) % M;
    }
    return (x + M) % M;
}

char s[11];
int mul[11];

void init()
{
    mul[0] = 1;
    rep(i, 1, 9)
        mul[i] = mul[i - 1] * 10;
}

int main()
{
    init();
    int t;
    sd(t);
    ll n;
    while (t--)
    {
        sld(n);
        int len = 0;
        ll tmp = n;
        while (tmp)
        {
            len++;
            tmp /= 10;
        }
        rep(i, len, 18)
        {
            ll ans = -1;
            rep(j, 1, i / 2)
            {
                if (i % j == 0) //循环节长度必定是数本身长度的约数但不包括数本身长度
                {
                    ll tmp = 1;
                    for (int k = 1; k < i / j; k++)
                    {
                        tmp = tmp * mul[j] + 1; //构造循环节长度为j的乘数
                    }
                    ll gcd = gcd(tmp, n);
                    ll x = n / gcd;
                    if (x < mul[j]) //循环节长度为j的,可以写成tmp*x(10*(j-1)<=x<=10*j),当然此处的x还不符合范围要求,只是最小的
                    {  
                        ll y = mul[j - 1] / x * x; //此处的x是最小值,但是要求x要在上述范围内,所以要进行处理
                        if (y < mul[j - 1])
                            y += x;                     //经过处理让y为x的倍数,且,y在要求范围内 10*(j-1)<=y<=10*j
                        if (ans == -1 || ans > y * tmp) //这里y是x的倍数,而x*tmp则会得到n与tmp的最小公倍数,所以y*tmp肯定是n,tmp的公倍数
                            ans = tmp * y;
                    }
                }
            }
            if (ans != -1)
            {
                pld(ans);
                break;
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值