C. Sad powers

You‘re given Q queries of the form (L, R).

For each query you have to find the number of such x that L ≤ x ≤ R and there exist integer numbers a > 0, p > 1 such that x = ap.

Input

The first line contains the number of queries Q (1 ≤ Q ≤ 105).

The next Q lines contains two integers LR each (1 ≤ L ≤ R ≤ 1018).

Output

Output Q lines — the answers to the queries.

Example

input

Copy

61 49 95 712 29137 5911 1000000

output

2103171111

Note

In query one the suitable numbers are 1 and 4.

提供一种容斥原理的思想。

我们要求不大于n的所有幂数的个数。

把注意力先放在幂这个东西上

ci=pow(n,(1/i))可以得到所有以i为幂,小于等于n的底数的个数

那么最终答案是否是c1+c2+c3+...+ck呢

不是,因为有重复(例如:c2,c3间2^6这个数是被重复计算过的,但我们发现c2,c3的重复的这些数正好是c6)

进一步分析来看,

两个幂数:

ci,cj(i,j均可分解为奇数个不同质数的乘积)间重复的数为ck(k=lcm(i,j),且k一定可以分解为偶数个不同质数的乘积)

ans=ci+cj-ck

三个幂数:(看图)

ans=ci+cj+ck-c(ij)-c(jk)-c(ik)+c(ijk)

这不就是容斥原理么!!!

所以具体操作上,先打个容斥表

对象是幂

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lb;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c)
{
    return min(min(a, b), c);
}
template <class T> inline T max(T a, T b, T c)
{
    return max(max(a, b), c);
}
template <class T> inline T min(T a, T b, T c, T d)
{
    return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d)
{
    return max(max(a, b), max(c, d));
}
#define scanf1(x) scanf("%d", &x)
#define scanf2(x, y) scanf("%d%d", &x, &y)
#define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define bug printf("***********\n");
#define mp make_pair
#define pb push_back
const int maxn = 10005;
// name*******************************
int a[100];
ll l,r,q;

// function******************************
ll sol(ll x)
{
    ll s=x?1:0;//大于1就先加上1
    for(ll i=2; (1ll<<i)<=x; i++)
        s-=a[i]*((int)pow((lb)x+0.5,(lb)1/i)-1);//记得减一,1不能参与计数了
    return s;
}
//***************************************
int main()
{
//    ios::sync_with_stdio(0);
//    cin.tie(0);
    // freopen("test.txt", "r", stdin);
    //  freopen("outout.txt","w",stdout);
    a[1]=1;

    for(int i=1; i<=50; i++)
        for(int j=i*2; j<=100; j+=i)
            a[j]-=a[i];
    cin>>q;
    while(q--)
    {
        cin>>l>>r;
        cout<<sol(r)-sol(l-1)<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/planche/p/8723678.html

时间: 2024-07-31 23:43:50

C. Sad powers的相关文章

Codeforces 955C Sad powers(数论)

Codeforces 955C Sad powers 题意 q组询问,每次询问给定L,R,求[L,R]区间内有多少个数可以写成ap的形式,其中a>0,p>1,1 ≤ L ≤ R ≤ 1e18. 思路 对于p>2的情况,由于随着指数p的增大,小于1e18的p次幂的数量会急剧减小,总数量的级别在1e6多左右,因此可预处理.L,R不超过1e18,可以直接枚举数字1-1e6,将每个数字不超过1e18的且不是平方数的p次幂推入数组中,排序去重.以便回答询问时可二分求数量. 对于p=2的情况,对上界

Codeforces 955C - Sad powers(数论 + 二分)

链接: http://codeforces.com/problemset/problem/955/C 题意: Q次询问(1≤Q≤1e5),每次询问给出两个整数L, R(1≤L≤R≤1e18),求所有符合条件的整数x的个数.条件为:L≤x≤R,x = a的p次方(a, p为整数且a>0, p>1). 分析: 一.当指数p=3时,底数a最多有1e6个,由于指数增加时底数收敛得很快,所以我们可以将p>=3时的所有x放进vector里排序去重(预处理),求x的个数的时候二分查找即可.二.对于p=

codeforces 955C - Sad powers

传送门 Q(1?≤?Q?≤?105)组询问,给定L.R (1?≤?L?≤?R?≤?1018).,求闭区间内有多少个数能表示为一个数的k次幂(k > 1) 对于k=2的情况可以直接求根做差,对于k>3的情况,由于所有的数数目很少,我们可以直接枚举出来.过程中注意判重和平方数(否则与情况1重复计算) 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vec

CodeForce-955C

C. Sad powerstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou're given Q queries of the form (L, R). For each query you have to find the number of such x that L ≤ x ≤ R and there exist integer

什么是SAD,SAE,SATD,SSD,SSE,MAD,MAE,MSD,MSE?

SAD(Sum of Absolute Difference)=SAE(Sum of Absolute Error)即绝对误差和 SATD(Sum of Absolute Transformed Difference)即hadamard变换后再绝对值求和 SSD(Sum of Squared Difference)=SSE(Sum of Squared Error)即差值的平方和 MAD(Mean Absolute Difference)=MAE(Mean Absolute Error)即平均绝

LightOJ 1132 - Summing up Powers 矩阵快速幂+排列组合

链接:http://lightoj.com/volume_showproblem.php?problem=1132 1132 - Summing up Powers PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given N and K, you have to find (1K + 2K + 3K + ... + NK) % 232 Input Input starts with an i

uva 10515 - Powers Et Al.(数论)

题目链接:uva 10515 - Powers Et Al. 题目大意:给出m和n,问说mn的个数上的数是多少. 解题思路:其实只要看m的最后一位数就可以了,判断最有一位的周期,然后用n%t即可. #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 15; const int maxs = 105; vector<int> g

Codeforces 622F The Sum of the k-th Powers(数论)

题目链接 The Sum of the k-th Powers 其实我也不懂为什么这么做的--看了无数题解觉得好厉害哇-- 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i, a, b) for (int i(a); i <= (b); ++i) 6 #define dec(i, a, b) for (int i(a); i >= (b); --i) 7 8 const int mod = 1

What Powers Instagram: Hundreds of Instances, Dozens of Technologies(译文,转)

add by zhj: 对译文略有修改.原文发表时,Instagram还没被Facebook收购,读完只感觉Instagram这三个后台工程师真牛逼. 三个人就可以搞定1400万注册用户.不过,另一方面,我们也看到,这三个人其实使用的都是现成的技术,至少从文章中看不出他 们有什么技术上的创新,当然就三个人搞创新也难了点,而且如果现有技术能基本上解决问题,对这样一个小团队而言,就没必要 自己开发新技术.最后,对他们愿意把方案分享出来表示非常感谢. 英文原文:http://instagram-eng