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\n", ans);
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%I64d%d", &n, &r);
        solve();
    }
    return 0;
}

fuck

时间: 2024-10-13 12:05:26

HDU 5297 Y sequence 容斥/迭代的相关文章

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数列

题意:给定正整数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范围的. 所以显然不用去枚举每一个数了,更不用说推断每一个数是不是某个数的某次方.那么这个题怎么下手呢.首先我们能够非常直观的想到,被删去的数显然是非常分散的.由于能表示成某个数的幂这种形式的数非

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

[多校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 然后就是一个很神奇的方法了,这个方法特别的秒 就是迭代的方法. 假

hdu4390-Number Sequence(容斥计算)

题意:给定b数列,计算有多少种数列 a1,a2,...,an 满足条件 a1*a2*...*an=b1*b2*-*bn (ai>1). 解法:处理出b数列中出现的所有质因子的数量记录在map中,然后进行容斥计算: 代码: /****************************************************** * author:xiefubao *******************************************************/ #pragma

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 3970 Harmonious Set 容斥欧拉函数

链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n 求连续整数[0,n), 中任意选一些数使得选出的数和为n的倍数的方法数 ...并不会如何递推.. 思路: 然后这是公式:点击打开链接 a(n) = 1/n * sum_{d divides n and d is odd} 2^(n/d) * phi(d). d最大是n,也就是1e9,要计算1e9的phi,所以容斥来算phi. #pragma comment(linker, "/STA

HDU 1695 GCD(容斥定理)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7529    Accepted Submission(s): 2773 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y