题解 CF1352C 【K-th Not Divisible by n】

实际发表时间:2020-05-12
https://www.luogu.com.cn/problem/CF1352C
求第\(k\)个不被\(n\)整除的数

Solution

首先可以想到暴力枚举,但是看到数据范围,肯定会TLE.
那么就得换一个思路了。
我们按每个正整数对\(n\)除的商和余可以得到一个表格:

1 2 \(\cdots\) \(n-1\) \(n\)
\(n+1\) \(n+2\) \(\cdots\) \(2n-1\) \(2n\)
\(\cdots\) \(\cdots\) \(\cdots\) \(\cdots\) \(\cdots\)
\(xn+1\) \(xn+2\) \(\cdots\) \((x+1)n-1\) \((x+1)n\)

由于我们只要不被\(n\)整除的,那么就把最后一列去掉:

1 2 \(\cdots\) \(n-1\)
\(n+1\) \(n+2\) \(\cdots\) \(2n-1\)
\(\cdots\) \(\cdots\) \(\cdots\) \(\cdots\)
\(xn+1\) \(xn+2\) \(\cdots\) \((x+1)n-1\)

现在就要求表格里的第\(k\)个数。不难发现,第\(k\)个数就在第\(x=\left\lceil\dfrac{k}{n-1}\right\rceil\)行第\(y=(k-1) \bmod (n-1) +1\)列,那么这个数就是\(n(x-1)+y\),直接计算即可

Code

#include<bits/stdc++.h>
using namespace std;
 
inline void readint(int &x)//快读
{
    int f=1;x=0;
    char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        x=(x<<3)+(x<<1)+ch-48;
        ch=getchar();
    }
    x*=f;
}
 
int t,n,k;
 
inline int myceil(int x,int y){return x%y?x/y+1:x/y;}//x/y的向上取整
 
int main()
{
    readint(t);
    while(t--)
    {
        readint(n),readint(k);
        int x=myceil(k,n-1),y=(k-1)%(n-1)+1;//计算行列
        printf("%d\n",n*(x-1)+y);//输出答案
    }
    return 0;
}
posted @ 2020-08-29 10:51  ADay526  阅读(119)  评论(0)    收藏  举报