Orac and Factors

Orac is studying number theory, and he is interested in the properties of divisors.

For two positive integers aa and bb, aa is a divisor of bb if and only if there exists an integer cc, such that a⋅c=ba⋅c=b.

For n≥2n≥2, we will denote as f(n)f(n) the smallest positive divisor of nn, except 11.

For example, f(7)=7,f(10)=2,f(35)=5f(7)=7,f(10)=2,f(35)=5.

For the fixed integer nn, Orac decided to add f(n)f(n) to nn.

For example, if he had an integer n=5n=5, the new value of nn will be equal to 1010. And if he had an integer n=6n=6, nn will be changed to 88.

Orac loved it so much, so he decided to repeat this operation several times.

Now, for two positive integers nn and kk, Orac asked you to add f(n)f(n) to nn exactly kk times (note that nn will change after each operation, so f(n)f(n) may change too) and tell him the final value of nn.

For example, if Orac gives you n=5n=5 and k=2k=2, at first you should add f(5)=5f(5)=5 to n=5n=5, so your new value of nn will be equal to n=10n=10, after that, you should add f(10)=2f(10)=2 to 1010, so your new (and the final!) value of nn will be equal to 1212.

Orac may ask you these queries many times.

Input

The first line of the input is a single integer t (1≤t≤100)t (1≤t≤100): the number of times that Orac will ask you.

Each of the next tt lines contains two positive integers n,k (2≤n≤106,1≤k≤109)n,k (2≤n≤106,1≤k≤109), corresponding to a query by Orac.

It is guaranteed that the total sum of nn is at most 106106.

Output

Print tt lines, the ii-th of them should contain the final value of nn in the ii-th query by Orac.

Sample Input

3
5 1
8 2
3 4

Sample Output

10
12
12

Note

In the first query, n=5 and k=1. The divisors of 5 are 1 and 5, the smallest one except 1 is 5. Therefore, the only operation adds f(5)=5f(5)=5 to 5, and the result is 10.

In the second query, n=8and k=2. The divisors of 8 are 1,2,4,8, where the smallest one except 1 is 2, then after one operation 8 turns into 8+(f(8)=2)=10. The divisors of 10 are 1,2,5,10, where the smallest one except 1 is 2, therefore the answer is 10+(f(10)=2)=12.

In the third query, nn is changed as follows: 3→6→8→10→123→6→8→10→12.

题目大意:

定义 f(n) 为 除了 1,n 之外的最小因子,需要进行 k次操作,每次将 n 加上 f(n)。

解题思路:

第一遍找约数,第二遍开始直接+2。从第二个开始,每个都和前面相差2,因此当k==1时,结果ans=n+f(n);当k>=2,ans=n+f(n)+(k-1)*2。

代码如下:
#include<iostream>
#include<algorithm>
#include<cstdio>

using namespace std;

int main() 
{
	int t;
	scanf("%d",&t);
	while (t--) 
	{
		int n,k;
		scanf("%d %d",&n,&k);
		if(n%2!=0)
		{
			for(int i=2;i<=n;i++) 
			{
				if(n%i==0) 
				{
					n+=i;k--;
					break; 
				}
			}
		}
		printf("%d\n",n+2*k);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值