zoukankan      html  css  js  c++  java
  • 【HDU】3208 Integer’s Power

    题意:定义27的权为3,16的权为4(3^3=27,2^4=16)。求一个区间中所有数的权值之和。

    很容易知道[1,n]中有多少个数可以表示成k次方的形式,即n^(1/k)个。

    但是,一个数的平方包含了4 6 8 10……次方。

    如果知道至多只能表示成k次方的数有多少个,那么就能得到答案了。

    可以倒着容斥,如果正的推太复杂了。

    另外,pow精度不够,二分答案吧。

     1 import java.util.*;
     2 import java.math.*;
     3 
     4 public class Main {
     5     static int MAXN = 60;
     6 
     7     static long Pow(long n, int k) {
     8         long low, high, mid, res;
     9         low = res = 1;
    10         high = n + 1;
    11         while (low < high) {
    12             mid = (low + high) >> 1;
    13             if (BigInteger.valueOf(mid).pow(k).compareTo(BigInteger.valueOf(n)) <= 0) {
    14                 low = mid + 1;
    15                 res = mid;
    16             } else
    17                 high = mid;
    18         }
    19         return res;
    20     }
    21 
    22     static long Count(long n) {
    23         long cnt[] = new long[MAXN];
    24         long ans;
    25         int i, j;
    26         for (i = 0; i < MAXN; i++)
    27             cnt[i] = 0;
    28         for (i = 1; i < MAXN; i++)
    29             cnt[i] = Pow(n, i);
    30         ans = 0;
    31         for (i = MAXN - 1; i > 0; i--) {
    32             for (j = i + i; j < MAXN; j += i)
    33                 cnt[i] -= cnt[j];
    34             ans += i * cnt[i];
    35         }
    36         return ans;
    37     }
    38 
    39     public static void main(String[] args) {
    40         Scanner in = new Scanner(System.in);
    41         long a, b;
    42         while (in.hasNext()) {
    43             a = in.nextLong();
    44             b = in.nextLong();
    45             if (a == 0 && b == 0)
    46                 break;
    47             System.out.println(Count(b) - Count(a - 1));
    48         }
    49     }
    50 }
  • 相关阅读:
    PYQT4 : QSystemTrayIcon练习
    PyQt4 初试牛刀一
    PyQt4简单小demo
    PyQt5实时汇率查询
    PyQt:昨天今天明天表示方法
    mybatis拓展框架对比
    docker部署oracle
    docker部署Jenkins,以及在Jenkins中使用宿主机的docker/docker-compose命令
    Jersey 出现415 MediaType is not supported问题的原因
    [转]前端常见跨域解决方案(全)
  • 原文地址:https://www.cnblogs.com/DrunBee/p/2675253.html
Copyright © 2011-2022 走看看