poj2109 这货是大数么

你以为它是大数,但实际上它不是

我觉得这应该是种可正常的姿势了

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
	static BigInteger p,l,r,div;
	static int n;
	public static int cmp(BigInteger mid){
		BigInteger sum=mid.pow(n);
		return sum.compareTo(p);
	}
	public static BigInteger calc(){
		l=BigInteger.ZERO;
		r=BigInteger.valueOf(1000000000);
		BigInteger div=BigInteger.valueOf(2);
		while(l.compareTo(r)<0){
			BigInteger mid=l.add(r).divide(div);
			int fl=cmp(mid);
			if(fl==0){
				return mid;
			}
			else if(fl==-1){
				l=mid.add(BigInteger.ONE);
			}
			else r=mid;
		}
		return r;
	}
	public static void main(String args[]){
		Scanner scanner=new Scanner(System.in);
		while(scanner.hasNext()){
			n=scanner.nextInt();
			p=scanner.nextBigInteger();
			BigInteger ans=calc();
			System.out.println(ans);
		}
	}
}

 但wa

有种更为优雅的姿势

#include <cstdio>
#include <cmath>

int main()
{
    double n , m ;
    int ans ;
    while ( scanf( "%lf%lf" , &m , &n ) != EOF )
          printf( "%.0f\n" , exp(log(n)/m) ) ;
}

感动地五体投地

另附大神证明思路:泰勒公式证明相差不会超过9

http://blog.csdn.net/synapse7/article/details/11672691

经过无聊的改动

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    static BigInteger p,l,r,div;
    static int n;
    public static int cmp(BigInteger mid){
        BigInteger sum=mid.pow(n);
        return sum.compareTo(p);
    }
    public static BigInteger calc(){
        l=BigInteger.ZERO;
        r=BigInteger.valueOf(1000000000);
        BigInteger div=BigInteger.valueOf(2);
        while(l.compareTo(r)<0){
            BigInteger mid=l.add(r).divide(div);
            int fl=cmp(mid);
            if(fl==0){
                return mid;
            }
            else if(fl==-1){
                l=mid.add(BigInteger.ONE);
            }
            else r=mid;
        }
        int fl=0;
        if((fl=cmp(r))==0)return r;
        if(fl==-1){
            while(p.subtract(r.pow(n)).compareTo(BigInteger.ONE)>0)r=r.add(BigInteger.ONE);
            return r;
        }
        else {
            while(r.pow(n).subtract(p).compareTo(BigInteger.ONE)>0)r=r.subtract(BigInteger.ONE);
            return r;
        }
    }
    public static void main(String args[]){
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()){
            n=scanner.nextInt();
            p=scanner.nextBigInteger();
            BigInteger ans=calc();
            System.out.println(ans);
        }
    }
}
时间: 2024-08-24 19:47:43

poj2109 这货是大数么的相关文章

POJ2109 高精度(含大数开方)+二分

1 高精度(含大数开方)+二分 一个技巧和三点注意: 技巧:假设k^n=p;(k的n次方),那么p的位数/n得到的是k的位数!例如:n=7,p=4357186184021382204544,p的位数为22,用22/7的结果向上取整,得到4,即为k的位数,也就是说k的取值范围是1000~9999.(引自code_pang)不利用这一点,高精度+直接二分,也会超时.用这一个技巧合理缩小二分的范围. 注意:看code的main中的注释. (二分思想不熟练,因为二分算法很高效,所以一定要暴力点直接确定l

【小程序】获取微信 自带的 收货地址获取和整理

1.wx.chooseAddress(OBJECT) if(wx.chooseAddress){ wx.chooseAddress({ success: function (res) { console.log(JSON.stringify(res)) }, fail: function(err){ console.log(JSON.stringify(err)) } }) }else{ console.log('当前微信版本不支持chooseAddress'); } 2.数据处理,仅供参考 /

PHP电商订单自动确认收货redis队列

一.场景 之前做的电商平台,用户在收到货之后,大部分都不会主动的点击确认收货,导致给商家结款的时候,商家各种投诉,于是就根据需求,要做一个订单在发货之后的x天自动确认收货.所谓的订单自动确认收货,就是在在特定的时间,执行一条update语句,改变订单的状态. 二.思路 最笨重的做法,通过linux后台定时任务,查询符合条件的订单,然后update.最理想情况下,如果每分钟都有需要update的订单,这种方式也还行.奈何平台太小,以及卖家发货时间大部分也是密集的,不会分散在24小时的每分钟.那么,

ECSHOP自动收货解决方案 【附代码】

ecshop系统,本身不带自动确认收货的,网上也找了一下,很多很复杂,且需要在服务器端设置定时任务,如果是虚拟主机,基本上就歇菜了. 某宝有一些卖自动收货的插件,不太了解其机制,不过也比较贵,要1-200一个.没办法,就自己开发吧.以下为解决方案. 解决思路: 跟淘宝的类似,默认设置7天后还没有自动确认收货,那么就会系统强制确认收货.只不过这个强制确认收货,没有在系统里设置常驻内存的程序,也没有设置系统自动任务,为变通的方案.设定为用户在登录成功后,进入用户中心页面,就会扫描自己名下,有没有超过

Super超级ERP系统---(9)订单管理--订单拣货

订单审核完成后,库房就可以开始备货,安排相应的人员去拣货了.订单拣货主要分为一次分拣和二次分拣,这里我们先看下一次分拣的流程.一次分拣就是根据订单去拣货,可以简单的理解为拿着一个订单,推着购物车,把当前订单放到购物车里面,这样就保证一个购物车只包含一个订单的商品.订单拣货首先要进行订单打印,然后分配给库房工作人员利用手持PDA进行拣货. 1.订单打印 订单打印界面列出需要拣货的订单,点击打印按钮进行打印 2.订单拣货         订单打印完成后,库内操作人员拿着手持PDA去拣货.把已经拣完的

HDU 4082 Hou Yi&#39;s secret-求相似三角形的最大个数-(坑货)

题意:找相似三角形的最大个数.注意不是所有相似三角形的个数,而是不同类相似三角形 中个数最大的 分析: 之前理解成了所有相似三角形的个数,所以尽管考虑了所有的特殊情况以及精度问题还是不停的wawawa,甚至重新写了一遍不用余弦来判断而是用边.绝望之中仔细看别人的代码,原来题意理解错了. 这题的收获: 1.三角形相似的判定:用余弦定理.或者边成比例.最好用边,然后判定的时候不用比值,用乘积,这样就不存在精度问题. 2.耐心.对自己有信心. 代码: #include<iostream> #incl

最短的计算大数乘法的c程序

#include <stdio.h> char s[99],t[99]; int m,n; void r(int i,int c) { int j=0,k=i; while(k)c+=s[j++]*t[k---1]; if(i)r(i-1,c/10); printf("%d",c%10); } void main() { gets(s);gets(t); while(s[n])s[n++]-=48; while(t[m])t[m++]-=48; r(m+n-1,0); }

light oj 1236 【大数分解】

给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,--em, 则结果为((1+2*e1)*(1+2*e2)--(1+2*em)+1)/2. //light oj 1236 大数分解素因子 #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <ctype.h> #i

nyoj 73 比大小 【java大数】

java大数. 代码: import java.util.Scanner; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(System.in); BigInteger a, b; BigInteger t = BigInteger.valueOf(0); a = cin.nextBigInteger(); b = cin.nextBi