首页 > 其他分享 >[atcode abc349] D - Divide Interval

[atcode abc349] D - Divide Interval

时间:2024-04-13 23:13:46浏览次数:12  
标签:IOException return Interval next static abc349 new atcode throws

  • 解决方法,贪心。
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        long L, R;
        L = rd.nextLong();
        R = rd.nextLong();

        PrintWriter out = new PrintWriter(System.out);
        List<long[]> ans = new ArrayList<>();
        if (L == 0) {
            ans.add(new long[]{0,1L << max2(R)});
            L = (1L<<max2(R));
        }
        long curr = L;

        while (curr < R) {
            int h = maxHighBit(curr);
            long diff = R - curr;

            long next = 0;
            if (max2(diff) >= h) {
                next = curr + (1L << h);
            } else {
                next = curr + (1L << max2(diff));
            }
            ans.add(new long[]{curr,next});
            curr = next;
        }
        out.println(ans.size());
        for (long[] tmp:ans) {
            out.println(tmp[0] + " " + tmp[1]);
        }
        out.flush();
        out.close();
    }

    public static int max2(long n) {
        for (int i = 0; i <= 61; i++) {
            if( (1L<<i)  > n) {
                return i - 1;
            }
        }
        return 0;
    }

    public static int maxHighBit(long n) {
        int num = 0;
        while (n % 2 == 0) {
            num++;
            n = n / 2;
        }
        return num;
    }
}

class rd {
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    // nextLine()读取字符串
    static String nextLine() throws IOException {
        return reader.readLine();
    }

    // next()读取字符串
    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    // 读取一个int型数值
    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    // 读取一个double型数值
    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    // 读取一个long型数值
    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    // 读取一个BigInteger
    static BigInteger nextBigInteger() throws IOException {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

标签:IOException,return,Interval,next,static,abc349,new,atcode,throws
From: https://www.cnblogs.com/fishcanfly/p/18133549

相关文章

  • AtCoder Beginner Contest 349
    A-ZeroSumGame(abc349A)题目大意\(n\)个人游戏,每局有一人\(+1\)分,有一人\(-1\)分。给定最后前\(n-1\)个人的分数,问第\(n\)个人的分数。解题思路零和游戏,所有人总分是\(0\),因此最后一个人的分数就是前\(n-1\)个人的分数和的相反数。神奇的代码n=input()prin......
  • AtCoder Beginner Contest 347
    A-Divisible#include<bits/stdc++.h>usingnamespacestd;usingi32=int32_t;usingi64=longlong;#defineinti64usingvi=vector<i64>;usingpii=pair<int,int>;usingpiii=tuple<int,int,int>;constintinf=1e......
  • atcoder beginer 347 (abc347) D E 题解
     D就是二进制下,哪些位置有重合(两个1),哪些位置没有重合(1个1,1个0),剩下的都是0。xor的结果<2^60,就是小于60位(二进制下)。注意要有要求两个数需要是2^60,于是要有大小的判断,因为有的a,b会2^60,但是按照题目要求,这个情况不行。比如xor的结果,60位都是1,然后a、b各有60个1,那么需要有30个1......
  • atcoder beginer 348 (abc348) D E F 题解
     E非常经典的树上操作(树上DP)。父节点到某个子节点,值是如何变化的。1#include<cstdio>2#include<cstdlib>3#include<cstring>4#include<cmath>5#include<cstdbool>6#include<string>7#include<algorithm>8#include<iost......
  • AtCoder Beginner Contest 348
    B-FarthestPoint难度:⭐题目大意一个坐标系有x个点,对于每个点找出距离其最远的点;解题思路数据很小,暴力就行;神秘代码#include<bits/stdc++.h>#defineintlonglong#defineIOSios::sync_with_stdio(false);cin.tie(0);cout.tie(0);#defineendl'\n'......
  • CF1909C Heavy Intervals 题解
    一种似乎更快抽象的解法?题面正文看这道题,给定序列\(l,r,c\),要求重构\(l,r,c\)使得\(\sum_{i=1}^n(r_i-l_i)\timesc_i\)最小。首先可以想到的就是尽量让小的\(r_i-l_i\)乘上大的\(c_i\)。这样子看来\(c_i\)几乎不需要更多的处理,仅需从小到大(或从大到小)排个序。来......
  • [ABC348] Atcoder ABC 248 A~G 题解
    [ABC348]AtcoderABC248A~G题解A模拟B模拟,不卡精度。C模拟D注意,药不可以拿着,只可以在那个格子吃掉。这就意味着,我们无论何时到达某个点,到达的点的集合都是固定的。所以对于每个药店跑BFS,然后看起点到终点是否连通即可。intn,m,k,ad[N][N],f[N][N],in[N][N],......
  • AtCoder Beginner Contest 348 A-F 题解
    A-PenaltyKickQuestion高桥将在一场足球比赛中获得\(N\)个点球。对于第\(i\)个罚球,如果\(i\)是\(3\)的倍数,他将罚球失败,否则罚球成功。请打印他罚球的结果。Solution当\(i\%3==0\)时说明能被\(3\)整除Code#include<bits/stdc++.h>usingnamespacest......
  • AtCoder Beginner Contest 348
    地址。赛时情况A、B题都很显然,C题大概推了好一会儿,最后还是做出来了。D题感觉十分难做,估计很难写,看了E。感觉还是不会,听说是原题,搜了一下,发现是树的重心,我还不会。直接贺题解,发现不对。修改了一下还是不对,最后发现INF取小了,过了。后面的不看了。赛后总结还行,跳过D......
  • web95 比较麻烦的interval绕过
    审计include("flag.php");highlight_file(__FILE__);if(isset($_GET['num'])){$num=$_GET['num'];if($num==4476){die("nonono!");}if(preg_match("/[a-z]|\./i",$num)){die......