HDU 5297

用x ^ (1 / n) 来求个数,容斥原理 , Num会向后移动, 迭代到不再变化,退出循环

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int Primes[] = {-2,-3,-5,-7,-11,-13,-17,-19,-23,-29,-31,-37,-41,-43,-47,-53,-59,-61,-67,-71};
vector<int> R_number;
void GetR(LL R) {
    R_number.clear();
    for(int i = 0; abs(Primes[i])<= R; ++i) {
        int tmp = Primes[i];
        int Num = R_number.size();
        for(int j = 0; j < Num; ++j) {
            if(abs(R_number[j]*tmp) <= 63)
            R_number.push_back(tmp * R_number[j]);
        }
        R_number.push_back(Primes[i]);
    }
}
LL Cnt(LL N) {
    //GetR(R);
    if(N == 1) return 0;
    LL cnt = N;
    for(int i = 0; i < R_number.size(); ++i) {
        LL tmp = LL (pow(N+0.5, 1.0/abs(R_number[i])) - 1);
        if(R_number[i] < 0) cnt -= tmp;
        else cnt += tmp;
    }
    return cnt - 1;
}
void Solve(LL N,LL R) {
    GetR(R);
    LL ans = N;
    while(1) {
        LL tmp = Cnt(ans);
        if(tmp == N) break;
        ans += N - tmp;
    }
    printf("%lld\n",ans);
}
int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        LL n,r;
        scanf("%I64d %I64d",&n,&r);
        Solve(n,r);
    }
    return 0;
}
时间: 2024-08-11 09:53:42

HDU 5297的相关文章

hdu 5297 Y sequence(容斥)

题目链接:hdu 5297 Y sequence 考虑62以内的指数,x为奇数个质数因子,就减掉,偶数个加上.计算x为指数的不满足数直接pow(n,1/x)即可. #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <algorithm> using namespace std; type

[2015hdu多校联赛补题]hdu 5297 Y sequence

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5297 题意:给你一个所有正整数的序列,然后去掉满足x^(2~r)的所有数(x为所有正整数,r>=2题目给出),问你现在这个序列的第n个数是什么 解:首先想到写一个函数func(y),它可以计算出给定数字y是序列中第几个数,这样我们大概可以二分答案~(事实上会TLE,得用迭代法,当然迭代的话也是用这个函数) 那么如何实现func: 首先想去掉满足x^2的所有数,我们可以用pow(y, 1/2)计算出y

HDU 5297 Y sequence 容斥/迭代

Y sequence Problem Description Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hates those numbers which can be written as a^b (a, b are positive integers,2<=b<=r),so he removed them all.Yellowstar ca

hdu 5297 容斥原理

很容易想到我们需要这样一个函数f(n)表示的是1~n的数字中在y sequence中的个数,于是可以想到用容斥来做,先假设答案是n然后计算n中y sequence的个数,然后n加上不够的,继续判断,一直迭代求出答案. 小技巧:预处理的时候素数设为负数方便判断容斥的时候是应该加还是减. 1 #include <iostream> 2 #include <cstring> 3 #include <vector> 4 #include <cstdio> 5 #in

HDU 5297 Y sequence

Y sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 658    Accepted Submission(s): 145 Problem Description Yellowstar likes integers so much that he listed all positive integers in ascen

HDU 5297(Y sequence-Mobius函数容斥+迭代)

Y sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1192    Accepted Submission(s): 265 Problem Description Yellowstar likes integers so much that he listed all positive integers in asce

HDU 5297 Y sequence Y数列

题意:给定正整数n和r.定义Y数列为从正整数序列中删除全部能表示成a^b(2 ≤ b ≤ r)的数后的数列,求Y数列的第n个数是多少. 比如n = 10. r = 3,则Y数列为2 3 5 6 7 10 11 12 13 14,第10个数是14. 非常有趣的一道数论题.题目给出的范围是long long范围的. 所以显然不用去枚举每一个数了,更不用说推断每一个数是不是某个数的某次方.那么这个题怎么下手呢.首先我们能够非常直观的想到,被删去的数显然是非常分散的.由于能表示成某个数的幂这种形式的数非

[多校2015.01.1010 容斥+迭代] hdu 5297 Y sequence

题意: 给你一个n和一个r,求Y序列的第N项是多少. 所谓的Y序列就是,从1开始,去掉能表示成a^b(2<=b<=r)的数,所构成的序列 例如r=2 序列就是:2,3,5,6,7,8,10,11,12,13,14,15,17.... 思路: 我们应该能想到需要一个函数fun(x) 求的是1~x内在Y序列里的数有多少个 这个其实不难,我们可以运用容斥原理,通过63以内的素数进行计算,并且最多做三遍,因为2*3*5*7>63 然后就是一个很神奇的方法了,这个方法特别的秒 就是迭代的方法. 假

2015 ACM多校训练第一场

在下面网址看效果更佳>_< http://mlz000.github.io/2015/08/07/2015-ACM%E5%A4%9A%E6%A0%A1%E8%AE%AD%E7%BB%83%E7%AC%AC%E4%B8%80%E5%9C%BA/ 题外话 这个暑假以前就决定要把这次多校的所有题全补了,中间断断续续,总算把第一场的题补全了,鄙视一下颓废的自己... hdu 5288(1001) OO's Sequence Solution 水题,定义两个数组L[i],R[i]示第i个数左侧和右侧最接