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-輪月

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