A - Divisibility Shortcut -Kattis - shortcut

**A - Divisibility Shortcut ***
添加链接描述
There is a well-known shortcut (or “trick”) for determining whether or not a non-negative integer n is divisible by 3:
Add up the digits in the base-10
representation of n; call this sum S10(n)。
If S10(n)is divisible by 3, then n is divisible by 3
It turns out that this idea can be generalized to other bases and divisors. Let b≥2
and d≥1 be integers. For any integer n≥0, define Sb(n) to be the sum of the digits in the base-b representation of n. We say that DS(b,d) holds (DS stands for “divisibility shortcut”) if the following is true: for all integers n≥0, if Sb(n) is divisible by d, then n is divisible by d
Given integers b≥2
and k≥1, find the largest integer d≤k such that DS(b,d)
holds.
Input
The first line of input contains an integer T
(1≤T≤100), the number of test cases. This is followed by T lines, one per test case, each of which contains two space-separated integers, b and k (2≤b≤109, 1≤k≤109)
Output
For each test case, output a single line containing the largest integer d≤k
such that DS(b,d)
holds.
Sample Input 1
2
10 5
24 11
Sample Output 1
3
1
题意:求最大d<=k,满足对于任何数如果以b为基数的各个位数上的和能整除d,则可以得到该数一定能整除d
解题思路:x0b0+x1b1+…+xmbm=ds,x0+x1+…+xm=dk,s和k为任意整数,将两式相减,得到x1(b-1)+x2(bb-1)+…+xm(b^m-1)=d(s-k),易知左边各项含有一个公倍数为b-1,所以d肯定为b-1的一个因数且小于k,该题就变成了找b-1的因数

AC代码:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<string>
#include<sstream>
#include<cmath>
#include<set>
#define maxn 1359
using namespace std;
typedef long long ll;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int b,k;
        scanf("%d%d",&b,&k);
        int pig=-1,ans=b-1,j=sqrt(ans+1);
        for(int i=1;i<=j;i++)
        {
            if(ans%i==0)
            {
                if(ans/i>k&&i<=k)
                    pig=max(pig,i);
                else if(ans/i<=k)
                    pig=max(pig,ans/i);
            }
        }
        printf("%d\n",pig);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Starry~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值