LeetCode:求能否找到一个全为1的正整数可以整除K

Given a positive integer K,you need to find the length of the smallest positive integer N 
such that N is divisible by K,and N only contains the digit 1.
Return the lenght of N.If there is no such N,return -1.
Note:N may not fit in a 64-bit signed integer.

Example 1:
Input:K=1
Output:1
Explantion:The smallest is N=1,which 1. answer has length.

Example 2:
Input:K=2
Output:-1
Explantion:There is no such by 2.positive integer N divisible.

Example 3:
Input:K=3
Output:3
Explantion:The smallest is N=111,which 3. answer has length.

Constraints:
1<=K<=10^5

题目大意:
给定一个正整数K,求能否找到一个全为1的正整数可以整除K,求这个最小的长度为多少?注意这个整数N可以大于64位有符号
的Interge正整数.

解题思路:
这个一道典型的数学问题,按照题目的要求从小到大进行遍历.记得小学老师讲过,能被2整除的正整数必定是偶数,末尾为0或者
为5的正整数必然可以整除5.这样就可以初始条件判断K数是否能整除2和5,若满足,直接返回-1.此外,需要注意的是,整除K的
数可能发生越界的情况,根据抽屉原理,应该是高中的知识,每次循环过程中对K进行求余数.这样就可以避免越界.而且遍历的
最大值应该设为K,若超过K则会重复循环之前的结果.
#include <iostream>

using namespace std;

class Solution{
public:
    int smallestRepunitDivByK(int K){
        if(K&1==0||K%5==0) return -1;
        int N=0;
        for(int i=1;i<=K;++i){
            N=(10*N+1)%K;//公示:G(n)=F(n)%K->F(n)=(F(n-1)*10+1)%K,G(n)=G(N-1)*10+1
            if(N==0) return i;
        }
        return -1;
    }
};
int main(int argc,char* argv[]){
    cout<<Solution().smallestRepunitDivByK(9)<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

路上的追梦人

您的鼓励就是我最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值