zoukankan      html  css  js  c++  java
  • [Neerc2015] Binary vs Decimal(java大数 + 找规律)

    题目大意是有一种数,它的二进制形式的后几位刚好是它自己。

    比如100的二进制形式是1100100,1100100的末尾三个数刚好是100,就符合条件,求第n个符合条件的数。

    序列在oeis A181891。

    做法的话先是发现这样的数的后缀一定在之前出现过,然后可以bfs一下。

    更简单的做法是发现一个规律,就是符合条件的数的二进制形式比如100的1100100,如果把1100100当成十进制在转换为二进制的话得到100001100100101000100,末尾依然是100,这样的话我们检查1100100的二进制末尾是不是100(通过位运算)就可以判断是不是符合条件的数,这一块的复杂度降下来以后就可以暴力搞了,配上java大数简直酸爽。

    import java.util.*;
    import java.math.*; 
    import java.io.*;
    public class Main {
        static BigInteger[] a, b;
        static PrintStream out;
        public static void main(String[] args) throws IOException{
            Scanner sc = new Scanner(new File("binary.in"));
            out = new PrintStream(new FileOutputStream(new File("binary.out")));
            int n = sc.nextInt();
            a = new BigInteger[n*3];
            b = new BigInteger[n*3];
            int m = 2;
            BigInteger Pow10 = BigInteger.valueOf(1);
            a[0] = b[0] = BigInteger.valueOf(0);
            a[1] = b[1] = BigInteger.valueOf(1);
            int i, j;
            for(i = 1; m <= n; i++){
                Pow10 = Pow10.multiply(BigInteger.valueOf(10));
                int t = m;
                for(j = 0; j < t; j++){
                    BigInteger ta, tb;
                    ta = a[j].add(BigInteger.ONE.shiftLeft(i));
                    tb = b[j].add(Pow10);
                    if(ta.xor(tb).and(
                            BigInteger.ONE.shiftLeft(i+1).subtract(BigInteger.ONE)
                            ).equals(BigInteger.ZERO)){
                        a[m] = ta;
                        b[m] = tb;
                        m++;
                    }
                }
            }
            out.println(a[n].toString(2));
        }
    }
  • 相关阅读:
    C++宏定义详解
    编写Qt Designer自定义控件 MyPlugins
    关于MFC共享DLL的模块状态切换 .
    QT 与 MFC 的区别 .
    typedef
    C++ floor函数
    C++ floor函数 截断浮点数小数部分 转
    MFC的多国语言界面的实现 转
    新工作 Day16 周五
    新工作 Day15 周四
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5683837.html
Copyright © 2011-2022 走看看