B. Div Times Mod

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasya likes to solve equations. Today he wants to solve \((x\ div\ k)⋅(x\ mod \ k)=n\), where divdiv and modmod stand for integer division and modulo operations (refer to the Notes below for exact definition). In this equation, \(k\) and \(n\) are positive integer parameters, and \(x\) is a positive integer unknown. If there are several solutions, Vasya wants to find the smallest possible \(x\). Can you help him?

Input

The first line contains two integers \(n\) and \(k\) (\(1≤n≤10^6\), \(2≤k≤1000\)).

Output

Print a single integer \(x\) — the smallest positive integer solution to \((x\ div\ k)⋅(x\ mod\ k)=n\). It is guaranteed that this equation has at least one positive integer solution.

Examples

input

Copy

6 3

output

Copy

11

input

Copy

1 2

output

Copy

3

input

Copy

4 6

output

Copy

10

Note

The result of integer division \(a\ div\ b\) is equal to the largest integer \(c\) such that \(b⋅c≤a\). \(a\) modulo \(b\) (shortened \(a\ mod\ b\)) is the only integer \(c\) such that \(0≤c<b\), and \(a−c\) is divisible by \(b\).

In the first sample, \(11\ div\ 3=3\) and \(11\ mod\ 3=2\). Since \(3⋅2=6\), then \(x=11\) is a solution to \((x\ div\ 3)⋅(x\ mod\ 3)=6\). One can see that \(19\) is the only other positive integer solution, hence \(11\) is the smallest one.


题解:

数学题 ---- 我的致命弱点

由 \((x\ div\ k)·(x\ mod\ k) = n\) 可得, \((x\ mod\ k)\) 是 \(n\) 的一个因数,则 n % \((x\ mod\ k)\) = 0,同样,n >= 1,所以 x != k 即不会 mod 得 0。

这样,我们就可以遍历 1 ~ k - 1 找 n % i == 0 的数,即 i == x mod k。同时这样也符合时间复杂度的范围。

得到 i == x mod k 后,可把 x div k 写成 \((x - i)\ /\ k\) (被除数、除数与余数的关系啦),这样式子就变成 ---- $(x - i)\ /\ k * i = n $ ,转换一下,得: \(x = n\ /\ i\ *\ k + i\)

需要注意一下是:题目要求输出最小的 \(x\) ,所以要使 \(x\) 最小,可以用 min(),但这里贪方便就直接从 k-1 开始查,这样就保证输出的定是最小的了。(除以一个正整数总比加上一个正整数要小得多吧?)


// https://codeforces.com/contest/1087/problem/B
#include<iostream>
#include<cstdio>
using namespace std; int n, k; int main()
{
scanf("%d %d", &n, &k);
for(int i = k - 1; i > 0; i--){
if(n % i == 0){
printf("%d\n", n / i * k + i);
break;
}
}
return 0;
}

【Codeforces】B. Div Times Mod的更多相关文章

  1. 【Codeforces】Round #491 (Div. 2) 总结

    [Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...

  2. 【Codeforces】Round #488 (Div. 2) 总结

    [Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...

  3. 【codeforces】【比赛题解】#960 CF Round #474 (Div. 1 + Div. 2, combined)

    终于打了一场CF,不知道为什么我会去打00:05的CF比赛…… 不管怎么样,这次打的很好!拿到了Div. 2选手中的第一名,成功上紫! 以后还要再接再厉! [A]Check the string 题意 ...

  4. 【Codeforces Round #422 (Div. 2) D】My pretty girl Noora

    [题目链接]:http://codeforces.com/contest/822/problem/D [题意] 有n个人参加选美比赛; 要求把这n个人分成若干个相同大小的组; 每个组内的人数是相同的; ...

  5. 【Codeforces】Codeforces Round #551 (Div. 2)

    Codeforces Round #551 (Div. 2) 算是放弃颓废决定好好打比赛好好刷题的开始吧 A. Serval and Bus 处理每个巴士最早到站且大于t的时间 #include &l ...

  6. 【codeforces】【比赛题解】#862 CF Round #435 (Div.2)

    这次比赛打得很舒服,莫名得了个Rank41,涨了219的Rating,就比较优秀.不过还是没有闫神厉害啊.题目链接::P. [A]MEX 题意: Evil博士把Mahmoud和Ehab绑架到了邪恶之地 ...

  7. 【codeforces】【比赛题解】#872 CF Round #440 (Div.2)

    链接. [A]寻找漂亮数字 题意: 给定了两列非零数字.我们说一个数是漂亮的,当它的十进制表达中有至少一个数从数列一中取出,至少有一个数从数列二中取出.最小的漂亮数字是多少? 输入: 第一行两个数\( ...

  8. 【codeforces】【比赛题解】#869 CF Round #439 (Div.2)

    良心赛,虽然我迟了半小时233333. 比赛链接:#869. 呃,CF的比赛都是有背景的……上次是<哈利波特>,这次是<物语>…… [A]巧妙的替换 题意: Karen发现了石 ...

  9. 【codeforces】【比赛题解】#868 CF Round #438 (Div.1+Div.2)

    这次是Div.1+Div.2,所以有7题. 因为时间较早,而且正好赶上训练,所以机房开黑做. 然而我们都只做了3题.:(. 链接. [A]声控解锁 题意: Arkady的宠物狗Mu-mu有一只手机.它 ...

随机推荐

  1. 美国 | USA B1/B2 十年签证到手记

    旅行生活美国 周一早上人还没到公司,就收到EMS快递员的电话,说有两份我的美国领事馆的签证快递,在前台要我签收一下. 美领馆的出签效率果然是高,上周三(8月10日)早上面签的,这三个工作日就把护照送到 ...

  2. Web应急:移动端劫持

    PC端访问正常,移动端访问出现异常,比如插入弹窗.嵌入式广告和跳转到第三方网站,将干扰用户的正常使用,对用户体验造成极大伤害. 现象描述 部分网站用户反馈,手机打开网站就会跳转到赌博网站. 问题处理 ...

  3. C#左移运算符

     << 左移操作符.简单理解是对变量进行与2的n次乘方的运算.比如x<<1=x*2x<<2=x*4x<<3=x*8x<<4=x*16依此类推 ...

  4. 【原创】闲来无事,用Winform写了个简易浏览器

    核心是利用了winform自带的WebBrowser控件,修改了下IE内核的版本,目前还是单线程的,逻辑挺简单的,萌新都能看懂. 废话不多说,上代码,附打包project. 链接:https://pa ...

  5. tkinter的单选Radiobutto

    from tkinter import * def printSelection(): num = var.get() if num == 1: lab.config(text="你是男生& ...

  6. javascript DOM拓展

    针对JS高级程序设计这本书,主要是理解概念,大部分要点源自书内.写这个主要是当个笔记加总结 存在的问题请大家多多指正! DOM拓展 1选择符 API 1.1 querySelector() 由docu ...

  7. vue工作原理分析

    初始化 在 new Vue() 时会调⽤用_init()进⾏行行初始化,会初始化各种实例例⽅方法.全局⽅方法.执⾏行行⼀一些⽣生命周期. 初始化props. data等状态.其中最重要的是data的「 ...

  8. 隐马尔科夫模型(Hidden Markov Models) 系列之一

    转自:http://blog.csdn.net/eaglex/article/details/6376826 介绍(introduction) 通常我们总是对寻找某一段时间上的模式感兴趣,这些模式可能 ...

  9. 【转】PHP利用Apache、Nginx的特性实现免杀Webshell

    转载备忘: get_defined_vars().getallheaders()是两个特性函数,我们可以通过这两个函数来构造我们的webshell. 前几天看到的,一直忘记写,填坑. 环境 函数 用法 ...

  10. 安恒Red Team 内部红蓝对抗框架

    0x00  准备钓鱼攻击(从公开资源) 1.常见的红队攻击向量和技术   2.常见的蓝队侦查和预防控制 0x02 发送钓鱼邮件(到目标组织员工邮箱地址) 1.常见的红队攻击向量和技术   2.常见的蓝 ...