二分(求l-r中的平方数)

题目描述

多次查询[l,r]范围内的完全平方数个数
定义整数x为完全平方数当且仅当可以找到整数y使得y*y=x

输入描述:
第一行一个数n表示查询次数
之后n行每行两个数l,r
输出描述:
对于每个查询,输出一个数表示答案

示例1

输入

5
1 3
1 4
2 4
4 4
1 1000000000

输出

1
2
1
1
31622

备注:
n <= 100000
0<= l <= r <= 1000000000

解题思路:暴力肯定会超时,所以很容易想到二分

#include<math.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[40000],len;
void init(){
    len=sqrt(1000000000.0)+1;
    for(int i=1;i<=len;i++)//先打表
        a[i]=i*i;
}
int binary_search(int key){
    int left=0,right=len,mid,ans;
    while(left<=right){
        mid=(left+right)/2;
        if(a[mid]<=key){
            ans=mid;
            left=mid+1;
        }
        else
            right=mid-1;
    }
    return ans;
}
int main()
{
    init();
    int n,l,r;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d",&l,&r);
        int x=binary_search(l);
        printf("%d\n",x);
        int y=binary_search(r);
        printf("%d\n",y);
        int ans=y-x+1;
        int t=sqrt(l*1.0);
        if(t*t!=l) //因为l是不是平方数,对最后结果有影响
        ans--;
        printf("%d\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lipu123/p/12150059.html

时间: 2024-08-01 09:11:08

二分(求l-r中的平方数)的相关文章

HDU XXXX:求[L,R]的素数数量(数位DP)

Problem G Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/131072K (Java/Other) Total Submission(s) : 62   Accepted Submission(s) : 28 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description S number is the number wh

求出数组中超过一半的数

Tango 是微软亚洲研究院的一个试验项目.研究院的员工和实习生们都很喜欢在 Tango上面交流灌水.传说,Tango 有一大"水王",他不但喜欢发贴,还会回复其他 ID 发的每个帖子.坊间风闻该"水王"发帖数目超过了帖子总数的一半.如果你有一个当前论坛上所有帖子(包括回帖)的列表,其中帖子作者的 ID 也在表中,你能快速找出这个传说中的 Tango 水王吗? 思路:首先想到的是一个最直接的方法,我们可以对所有 ID 进行排序.然后再扫描一遍排好序的 ID 列表,统

POJ 3273-Monthly Expense(二分求最小和中的最大)

Monthly Expense Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3273 Appoint description:  System Crawler  (2015-05-28) Description Farmer John is an astounding accounting wizard and has realize

SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax

275. To xor or not to xor The sequence of non-negative integers A1, A2, ..., AN is given. You are to find some subsequence Ai 1, Ai 2, ..., Ai k (1 <= i 1 < i 2 < ... < i k<= N) such, that Ai 1 XOR Ai 2 XOR ... XOR Ai k has a maximum value.

BZOJ2440(全然平方数)二分+莫比乌斯容斥

题意:全然平方数是指含有平方数因子的数.求第ki个非全然平方数. 解法:比較明显的二分,getsum(int middle)求1-middle有多少个非全然平方数,然后二分.求1-middle的非全然平方数个数能够用总数减掉全然平方数个数.计算全然平方数的个数用容斥: 首先加上n/(2*2)+n/(3*3)+n/(5*5)+n/(7*7)...+...然后减掉出现两次的,然后加上三次的...奇加偶减.这就是mou的原型,用mou数组计算非常easy: 代码: /*****************

平方数

package CHobery; /** * 给定7个数字2,3,5,6,7,8,9的全排列共7!个 * 7位数中有多少个平方数.试着求出这些7位平方数 * @author Administrator * */ public class Main1 { public static long b = 0,c = 0,d = 0,y = 0,g = 0; public static int m = 0; public static int n = 0; public static int k = 0

【python】升序查找100内最大平方数的方法

from math import sqrt #导入math模块中的sqrt函数(开平方) l=[]                   #定义一个空的列表l for i in range(1,100):     n=sqrt(i)     if n==int(n):         l.append(i)   #将平方数追加到l列表里      print l                print max(l)          #方法一:取列表最大值 print l[len(l)-1]  

UPC 2224 Boring Counting (离线线段树,统计区间[l,r]之间大小在[A,B]中的数的个数)

题目链接:http://acm.upc.edu.cn/problem.php?id=2224 题意:给出n个数pi,和m个查询,每个查询给出l,r,a,b,让你求在区间l~r之间的pi的个数(A<=pi<=B,l<=i<=r). 参考链接:http://www.cnblogs.com/zj62/p/3558967.html #include <iostream> #include <cstdio> #include <cstring> #incl

查询数组里有多少个数在[L,R]范围中(二分)

使用两次二分即可得到这个值 比如现在有一个vector<int> vec,里面存放的是有序数列. 我们现在希望找出范围在[L,R]之间的数有多少个. 则有cnt = upper_bound(vec.begin(),vec.end(),r) - lower_bound( vec.begin(),vec.end(),l) 这么多个. 比如这个题就可以用二分http://codeforces.com/problemset/problem/220/B 代码 1 #include <bits/st