Codeforces 955C Sad powers(数论)

9 篇文章 0 订阅
1 篇文章 0 订阅

题目链接:http://codeforces.com/problemset/problem/955/C

C. Sad powers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 L, R each (1 ≤ L ≤ R ≤ 1018).

Output

Output Q lines — the answers to the queries.

Example
Input
Copy
6
1 4
9 9
5 7
12 29
137 591
1 1000000
Output
2
1
0
3
17
1111
Note

In query one the suitable numbers are 1 and 4.

题解:直接暴力存a ^ p(p>=3),这样的数,并且这些数里不能使完全平方数。

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;

#define x0 x0___
#define y0 y0___
#define pb push_back
#define SZ(X) ((int)X.size())
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define ALL(X) X.begin(),X.end()
#define RALL(X) X.rbegin(),X.rend()
#define rep(i,j,k) for(int i = j;i <= k;i ++)
#define per(i,j,k) for(int i = j;i >= k;i --)
#define mem(a,p) memset(a,p,sizeof(a))


const ll MOD = 1E9 + 7;
ll qmod(ll a,ll b,ll c) {ll res=1;a%=c; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%c;a=a*a%c;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

template<typename T, typename S>
void upmax(T& a,S b){if(a<b) a=b;}
template<typename T, typename S>
void upmin(T& a,S b){if(a>b) a=b;}
template<typename T>
void W(T b){cout << fixed;cout << b << endl;}
void gettle() {while(1);}
void getre() {int t=0;t/=t;}


/
/
/
/
const ll U = 1E18;
vector<ll> V;
bool is_sqr(ll n)
{
    ll t = sqrt(n);
    return (t + 1) * (t + 1) == n || t * t == n || (t - 1) * (t - 1) == n;
}
void init()
{
    for(ll i = 2;i <= 1000000;i ++) {
        ll a = i * i;
        while(a <= U / i) {
            if(!is_sqr(a * i)) V.pb(a * i);
            a *= i;
        }
    }
    sort(ALL(V));
    V.erase(unique(ALL(V)),end(V));
}
ll sqrnum(ll n)
{
    ll l = 1, r = 1E9;
    while(l <= r) {
        ll mid = (l + r) >> 1;
        if(mid * mid <= n) l = mid + 1;
        else r = mid - 1;
    }
    return r;
}
int main()
{
    init();
    int Q;
    scanf("%d", &Q);
    ll L,R;
    while(Q --) {
        scanf("%lld %lld", &L, &R);
        ll res = sqrnum(R) - sqrnum(L - 1);
        res += (upper_bound(ALL(V),R) - lower_bound(ALL(V), L)); //求有序数组里有多少数在[L,R]里。
        printf("%lld\n", res);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值