1352C - K-th Not Divisible by n

题目链接


题目大意:

输出第k个不能被n整除的数。

例:n=3 k=7      不能被n整除的数是1 2 4 5 7 8 10 11 13 ....,答案为10。


题解:

我们还原下案例

1.当ans到7时发现7前面有两个数可以被n整除,所以ans+2;      记录now=6;

2.当ans在区间{7,9] 发现9也能被n整除,所以ans+1;               记录now=9;             

3.当ans在区间{9.10] 并没有数可以被n整除,所以输出ans;

同理我们只要找出ans上一次能被整除的位置now,然后每次判断区间内是否存在能被n整除的数,对ans进行加法。


#include <iostream>
using namespace std;
typedef long long int ll;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		cin >> n >> k;
		int ans = k;
		int now = 0;
		while ((ans - now) / n != 0)
		{
			int x = (ans - now) / n;
			now += x * n;
			ans += x;
		}
		cout  << ans << endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值