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 calls the sequence that formed by the rest integers“Y sequence”.When r=3,The first few items of it are:
2,3,5,6,7,10......
Given positive integers n and r,you should output Y(n)(the n-th number of Y sequence.It is obvious that Y(1)=2 whatever r is).
 
Input
The first line of the input contains a single number T:the number of test cases.
Then T cases follow, each contains two positive integer n and r described above.
n<=2*10^18,2<=r<=62,T<=30000.
 
Output
For each case,output Y(n).
 
Sample Input
2 10 2 10 3
 
Sample Output
13 14
 

题意:

    给定正整数n和r,定义Y数列为从正整数序列中删除所有能表示成a^b(2 ≤ b ≤ r)的数后的数列,求Y数列的第n个数是多少。

题解:

    假设我们知道了cal(x)表示包括x在内的x之前这个序列有多少个数,这个要容斥

    我们则二分就好了,但是不要二分,为什么呢,

    没有去写二分

    总之题解说了二分超时,那么我们不要二分,迭代过去就是了

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1e5+20, M = 30005, mod = 1000000007, inf = 0x3f3f3f3f;
typedef long long ll;
//不同为1,相同为0

//用负数方便计算容斥的符号
const int zz[19] = {-2, -3, -5, -7, -11, -13, -17, -19, -23, -29, -31, -37, -41, -43, -47, -53, -59, -61, -67};
ll n;
int r;
vector <int> G;
void init()
{
    G.clear();
    for(int i = 0; abs(zz[i]) <= r; i++)
    {
        int temp = G.size();
        for(int j = 0; j < temp; j++)
        {
            if(abs(zz[i]*G[j]) <= 63)
                G.push_back(zz[i]*G[j]);
        }
        G.push_back(zz[i]);
    }
}
ll cal(ll x)
{
    if(x == 1)
        return 0;
    ll ans = x;
    for(int i = 0; i < G.size(); i++)
    {
        ll temp = (ll)(pow(x + 0.5, 1.0/abs(G[i]))) - 1;
        if(G[i] < 0)
            ans -= temp;
        else
            ans += temp;
    }
    return ans - 1;
}

void solve()
{
    init();
    ll ans = n;
    while(1)
    {
        ll temp = cal(ans);
        if(temp == n)
            break;
        ans += n - temp;
    }
    printf("%I64d
", ans);
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%I64d%d", &n, &r);
        solve();
    }
    return 0;
}
fuck
原文地址:https://www.cnblogs.com/zxhl/p/5281492.html