A. k-th divisor(水题,数论,sqrt运用)

You are given two integers n and k. Find k-th smallest divisor of n, or report that it doesn't exist.

Divisor of n is any such natural number, that n can be divided by it without remainder.

Input

The first line contains two integers n and k (1 ≤ n ≤ 10151 ≤ k ≤ 109).

Output

If n has less than k divisors, output -1.

Otherwise, output the k-th smallest divisor of n.

Examples
input
4 2
output
2
input
5 3
output
-1
input
12 5
output
6
Note

In the first example, number 4 has three divisors: 12 and 4. The second one is 2.

In the second example, number 5 has only two divisors: 1 and 5. The third divisor doesn't exist, so the answer is -1.



思路:
将n的因子用sqrt分成两份。水过。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
int a[100000];
int main()
{
    LL n,m,sum=0,l=0;
    cin>>n>>m;
    if(n<m)
    {
        cout<<"-1"<<endl;
        return 0;
    }
    int s=(int)sqrt(n);
    int flag=0;
    for(int i=1; i<=s; i++)
    {
        if(n%i==0)
        {
            a[++l]=i;
            if(l==m)
            {
                flag=1;
                break;
            }
        }
    }
    if(flag)
    {
        cout<<a[l]<<endl;
        return 0;
    }
    int tmp=l;
    LL p;
    for(int i=l; i>=1; i--)
    {
        p=n/a[i];
        if(p!=a[l])
            ++tmp;
        if(tmp==m)
        {
            flag=1;
            break;
        }
    }
    if(flag)
        cout<<p<<endl;
    else
        cout<<"-1"<<endl;
}


### 使用 `BigDecimal.remainder` 方法进行除法运算并获取余数 在 Java 中,`BigDecimal` 类提供了多种用于执行精确算术操作的方法。其中,`remainder(BigDecimal divisor)` 是一种专门用来计算两个数值相除后的余数的方法[^1]。 以下是关于该方法的一些重要说明: - **功能描述**: `remainder` 方法返回的是被除数减去除数乘以商的结果(即 `this.subtract(divisor.multiply(this.divideToIntegralValue(divisor)))`)。这与整型数据中的取模操作有所不同。 - **精度控制**: 如果需要更高的精度或特定舍入模式,则可以使用重载版本的 `remainder(BigDecimal divisor, MathContext mc)` 来指定舍入上下文[^4]。 #### 示例代码 以下是一个完整的示例程序展示如何利用 `BigDecimal.remainder()` 计算两数之间的余数: ```java import java.math.BigDecimal; import java.math.RoundingMode; public class BigDecimalRemainderExample { public static void main(String[] args) { // 定义两个大十进制数 BigDecimal dividend = new BigDecimal("10.5"); BigDecimal divisor = new BigDecimal("3"); // 调用 remainder 方法得到余数 BigDecimal remainderResult = dividend.remainder(divisor); System.out.println("Dividend: " + dividend); System.out.println("Divisor: " + divisor); System.out.println("Remainder using default rounding: " + remainderResult); // 使用自定义舍入方式再次尝试 BigDecimal customRoundedRemainder = dividend.remainder(divisor, new java.math.MathContext(10, RoundingMode.HALF_UP)); System.out.println("Custom rounded Remainder: " + customRoundedRemainder); } } ``` 上述代码片段展示了两种调用形式下的结果差异——默认行为以及通过设置不同的 `MathContext` 参数来改变其表现的行为[^2]。 需要注意的一点是,在某些情况下可能会遇到异常情况比如当分母为零时会抛出 ArithmeticException 异常[^3]。 ### 结论 综上所述,虽然 `BigDecimal.remainder(...)` 提供了一种有效的方式来处理涉及高精度浮点数场景下求其余数的需求,但在实际应用过程中仍需注意输入参数的有效性和可能引发的各种边界条件错误等问
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值