2021牛客寒假算法基础集训营2 D .牛牛与整除分块(打表规律)

题目:
在这里插入图片描述

题解:
这题如果直接用整除分块写会t,可惜比赛中我打了表也没看出规律。o(╥﹏╥)o
可以发现,对于x<sqrt(n),答案一定是x。再可以发现,1到sqrt(n)之间的数一定会出现,那么我们算出n/x是这些数中的第几大,因为是对称的,所以可以算出另一半有多少个数,加上去就是答案。
可以看这幅图理解。
在这里插入图片描述
代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int>pii;
const int MAXN=1e5+5;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const double pi=acos(-1);
int main()
{
    
    
    int t;
    cin>>t;
    while(t--){
    
    
        int n,x;
        cin>>n>>x;
        if(x<sqrt(n)){
    
    
            printf("%d\n",x);
            continue;
        }
        int cnt;
        
        int res=sqrt(n);
        if(n/res==res){
    
    
            
            cnt=res-1;
        }
        else{
    
    
            cnt=res;
        }
        int c=n/x;
        printf("%d\n",res-c+1+cnt);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45755679/article/details/113621026