【Educational Codeforces Round 17 A题】有所不同的大暴力

来源:互联网 发布:淘宝注册还要拨打号码 编辑:程序博客网 时间:2024/06/12 00:45

题目要求n的第k小的约数。
于是就爆搜嘛。

#include <cstdio>#include <cmath>//#include <iostream>using namespace std;//const long long MAXN = 1e9;//long long a[MAXN];long long n;int cnt = 0;int main(){    int k;    cin >> n >> k;    for (long long i = 1; i <= n; i++)    {        if (n % i == 0)        {            cnt++;            if (cnt == k)            {                cout << i << endl;                return 0;            }        }    }    printf("-1\n");    return 0;}

这是一开始写的代码,在第5个点T了。。。。

后来其他题都不会,就又写了一份

#include <cstdio>#include <vector>#include <algorithm>#include <iostream>#include <cmath>#include <cstring>using namespace std;//bool is[1e15 + 2];vector<long long> a;int main(){    //memset(is, true, sizeof(is));    long long n;    int k;    cin >> n >> k;    for (int i = 1; i <= sqrt(n); i++)    {        if (n % i == 0)        {            a.push_back(i);            a.push_back(n / i);        }    }    sort(a.begin(), a.end());    if (k > (int)a.size())    {        printf("-1\n");    }    else    {        cout << a[k - 1] << endl;    }    return 0;}

遍历到它的一半,还用vector【注意vector的sort,之前我还不是很会用】。这次A了,算是有点技巧吧。。。(:з」∠)

之前还想到了用类似埃筛的思想,找到第一个不是它的约数的数,把那个数的倍数全都设成不是它的约数,但是我埃筛。。。。不怎么会写啊。。。欧拉筛我也不怎么会写qnq 就没写。
这个要下次好好看看把它写熟练啊。

0 0
原创粉丝点击