**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;
}