2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest —— E. Equal Digits

本文介绍了一个算法问题,针对给定的整数N和数字D,寻找最小的整数K(K≥2),使得N在以K为基数的数制中表示时,在末尾拥有尽可能多的连续数字D。文章提供了完整的解决思路及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E. Equal Digits
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

For the given integer N and digit D, find the minimal integer K ≥ 2 such that the representation ofN in the positional numeral system with baseK contains the maximum possible consecutive number of digitsD at the end.

Input

The input contains two integers N and D (0 ≤ N ≤ 1015,0 ≤ D ≤ 9).

Output

Output two integers: K, the answer to the problem, andR, the the number of consecutive digits D at the end of the representation of N in the positional numeral system with baseK.

Sample test(s)
Input
3 1
Output
2 2
Input
29 9
Output
10 1
Input
0 4
Output
2 0
Input
90 1
Output
89 2

                                                               

题意:

给出n 和 d , 要求得到用k 进制得到n,最后一个数要求为d,求出从后数d 的数量,要求数量最多是k 尽可能小。

思路:

要使n - d 用k 进制表示出,所以求出n-d 的因子,暴力枚举得到最大答案。

特殊情况是:n== d 和n< d

CODE:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
vector<ll> v;
ll n, d;
void work(ll nn)
{
    for(ll i = 1; i * i <= nn; ++i){
        if(nn%i == 0){
        if(i > d)
            v.push_back(i);
        if((nn/i) > d && i*i != nn)
            v.push_back(nn/i);
        }
    }
}
int main()
{
    //freopen("in", "r", stdin);
    while(~scanf("%I64d %I64d", &n, &d)){
        if(n == d){
            if(n <= 1) printf("2 1\n");
            else printf("%I64d 1\n",n + 1);
            continue;
        }
        if(n < d){
            printf("2 0\n");
            continue;
        }

        v.clear();
        work(n - d);
        ll r = 0, k = 2;
        for(ll i = 0; i < v.size(); ++i){
            ll n1 = n, s = 0;
            if(v[i] > 1){
                while(n1){
                    if(n1 % v[i] != d) break;
                    else{
                        s++;
                        n1 = n1/v[i];
                    }
                }
                if(s > r){
                    r = s;
                    k = v[i];
                }
                if(s == r && v[i] < k){
                    k = v[i];
                }
            }
        }
        printf("%I64d %I64d\n", k, r);
    }
    return 0;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值