Codeforces-K-th Not Divisible by n

山再高,往上爬,总能登顶;
路再长,走下去,定能到达。

Codeforces-K-th Not Divisible by n

题目描述

You are given two positive integers n and k. Print the k-th positive integer that is not divisible by n.
For example, if n=3, and k=7, then all numbers that are not divisible by 3 are: 1,2,4,5,7,8,10,11,13…. The 7-th number among them is 10.

输入

The first line contains an integer t (1≤t≤1000) — the number of test cases in the input. Next, t test cases are given, one per line.
Each test case is two positive integers n (2≤n≤109) and k (1≤k≤109).

输出

For each test case print the k-th positive integer that is not divisible by n.

Sample Input

6
3 7
4 12
2 1000000000
7 97
1000000000 1000000000
2 1

Sample Output

10
15
1999999999
113
1000000001
1

题目大意

寻找第k个不能被n整除的整数

思路分析

这个呢,如果给你一个数,你很容易判断出这是第几个不能被整除的数。
当然了给你一个可以整除的数,你可能也会判断出结果
比如n=3时,判断9可能判断出是第6个(按照我的方法),实际上9之后的第一个不能整除3的数才是第6个
我的方法是啥呢
很显然,我很容易找出来在我之前有多少个数能被整除
这个数是num那么就有num/n(向下取整)个数可以被n整除那么就能知道当前是第num-num/n个数可以被整除
然后你会发现这个是第几个数是呈现单调关系,很显然可以二分。但是得出的答案不一定是答案,有可能是可以整除的那个数。
在这里插入图片描述

AC时间到

#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<utility>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#pragma warning(disable:4244)
#define PI 3.141592653589793
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll read() {
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
inline void out(ll a) {
	if (a < 0)putchar('-'), a = -a;
	if (a >= 10)out(a / 10);
	putchar(a % 10 + '0');
}
ll phi(ll n)
{
	ll ans = n, mark = n;
	for (ll i = 2; i * i <= mark; i++)
		if (n % i == 0) { ans = ans * (i - 1) / i; while (n % i == 0)n /= i; }
	if (n > 1)ans = ans * (n - 1) / n; return ans;
}
ll qpow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod;
		n >>= 1;
	}
	return res;
}
#define Floyd for(int k = 1; k <= n; k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
#define read read()
ll n, m;
bool judge(ll mid)
{
	ll id = mid / n;
	id = mid - id;//判断id
	if (id >= m)return 1;
	return 0;
}
int main()
{
	int T = read;
	while (T--)
	{
		n = read, m = read;
		ll l = 0, r = 1e18;
		ll ans = 0;
		while (l <= r)
		{
			ll mid = (l + r) >> 1;
			if (judge(mid))
			{
				ans = mid;
				r = mid - 1;
			}
			else l = mid + 1;
		}
		while (ans % n == 0)ans++;
		out(ans);
		puts("");
	}
}

By-轮月

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章