D 牛牛与整除分块(打表找规律)

传送门


题目大意

给定 n n n,求出集合 S = { ⌊ x i ⌋ ∣ i ∈ [ 1 , n ] } S = \{\lfloor \frac{x}{i}\rfloor | i \in[1,n]\} S={ixi[1,n]},然后对集合 S S S去重并降序排序得到新集合 S ′ S' S。给定 x x x,求 ⌊ x i ⌋ \lfloor \frac{x}{i}\rfloor ix在集合 S ′ S' S中是第几大。

解题思路

首先1-100打表每个数对应的集合,然后就是找规律了,这个题如何找呢,首先不难发现 x x x比较小时,那么 x x x是几答案就是几,测试几个数后不难发现这个界限在 n \sqrt{n} n 附近,先假设为 n \sqrt{n} n ,当 x x x大于界限时,有时候会有 n \sqrt{n} n 个数,有时候会有 n − 1 \sqrt{n} - 1 n 1个数,然后我们可以发现当一个数恰为平方数时,其后面的 n − 1 \sqrt{n}-1 n 1个数和它一样,答案为 2 ∗ n − ⌊ n x ⌋ 2*\sqrt{n} - \lfloor \frac{n}{x} \rfloor 2n xn,否则答案为 2 ∗ n − ⌊ n x ⌋ + 1 2*\sqrt{n} - \lfloor \frac{n}{x} \rfloor + 1 2n xn+1

在这里插入图片描述


//
// Created by Happig on 2021/2/21.
//
#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;
#define ENDL "\n"
#define lowbit(x) (x & (-x))
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double dinf = 1e300;
const ll INF = 1e18;
const int Mod = 1e9 + 7;
const int maxn = 2e5 + 10;

set<int> s[105];

void table() {
    for (int i = 1; i <= 100; i++) {
        for (int j = 1; j <= i; j++) {
            s[i].insert(i / j);
        }
        cout << i << ": ";
        for (auto j = s[i].rbegin(); j != s[i].rend(); j++) cout << *j << " ";
        cout << endl;
    }
}

int getAns(int n, int x) {
    int cnt = 1, cur = n / x;
    for (auto j = s[n].rbegin(); j != s[n].rend(); j++, cnt++) {
        if (*j == cur) {
            return cnt;
        }
    }
    return 0;
}

int solve(int n, int x) {
    int m = sqrt(n);
    int temp;
    if (m * m == n || n - m * m < m) {
        temp = m;
    } else temp = m + 1;
    if (x <= temp) {
        return x;
    } else {
        return temp + m - n / x;
    }
}

void duipai() {
    for (int i = 1; i <= 100; i++) {
        for (int j = 1; j <= i; j++) {
            if (getAns(i, j) != solve(i, j)) {
                cout << i << " " << j << endl;
            }
        }
    }
}

int main() {
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
//    table();
//    duipai();
//    cout << "finish duipai" << endl;
    int t, n, x;
    cin >> t;
    while (t--) {
        cin >> n >> x;
        int m = sqrt(n);
        int temp;
        if (m * m == n || n - m * m < m) {
            temp = m;
        } else temp = m + 1;
        if (x <= temp) {
            cout << x << ENDL;
        } else {
            cout << temp + m - n / x << ENDL;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值