hdu 1316 How Many Fibs?
1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner sc =new Scanner(System.in); 7 BigInteger f[]=new BigInteger[530]; 8 f[1]=new BigInteger("1"); 9 f[2]=new BigInteger("2"); 10 for(int i=3;i<=520;i++) 11 { 12 f[i]=f[i-2].add(f[i-1]); 13 //System.out.println(i+" "+f[i]); 14 } 15 while(sc.hasNextBigInteger()) 16 { 17 BigInteger n,m,zero; 18 zero=new BigInteger("0"); 19 n=sc.nextBigInteger(); 20 m=sc.nextBigInteger(); 21 if(n.compareTo(zero)==0&&m.compareTo(zero)==0) 22 break; 23 int left=1,right=520,mid=0; 24 int best=100000; 25 while(left<=right) 26 { 27 28 mid=(left+right)/2; 29 if(f[mid].compareTo(n)>=0) 30 { 31 if(mid<best) 32 best=mid; 33 right=mid-1; 34 35 } 36 else 37 { 38 left=mid+1; 39 } 40 } 41 int ansl,ansr; 42 ansl=best; 43 left=0;right=520; 44 best=0; 45 while(left<=right) 46 { 47 48 mid=(left+right)/2; 49 50 if(f[mid].compareTo(m)<=0) 51 { 52 if(best<mid) 53 best=mid; 54 left=mid+1; 55 56 } 57 else 58 { 59 right=mid-1; 60 } 61 } 62 ansr=best; 63 System.out.println(ansr-ansl+1); 64 //System.out.println(ansl); 65 //System.out.println(ansr); 66 } 67 } 68 }
hdu 1063 n^m舍去多余的0
1 import java.math.BigDecimal; 2 import java.math.BigInteger; 3 import java.util.Scanner; 4 5 6 public class Main { 7 public static void main(String[] args) { 8 BigDecimal n,ans; 9 10 Scanner sc=new Scanner(System.in); 11 while(sc.hasNextBigDecimal()) 12 { 13 int m; 14 n=sc.nextBigDecimal(); 15 m=sc.nextInt(); 16 ans=new BigDecimal("1"); 17 for(int i=0;i<m;i++) 18 ans=ans.multiply(n); 19 ans=ans.stripTrailingZeros(); 20 String str=ans.toPlainString(); 21 if(str.startsWith("0.")) 22 str=str.substring(1); //以从1开始的子串为新串
23 System.out.println(str); 24 25 26 27 } 28 29 30 31 } 32 33 }
hdu 1047
1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 5 public class Main { 6 public static void main(String[] args) { 7 BigInteger ans,n,one,i,zero,m; 8 Scanner sc=new Scanner(System.in); 9 n=sc.nextBigInteger(); 10 zero=new BigInteger("0"); 11 12 one=new BigInteger("1"); 13 while(n.compareTo(zero)>0) 14 { 15 ans=new BigInteger("0"); 16 while(true) 17 { 18 m=sc.nextBigInteger(); 19 if(m.compareTo(zero)==0) 20 break; 21 ans=ans.add(m); 22 } 23 System.out.println(ans); 24 n=n.subtract(one); 25 if(n.compareTo(zero)!=0) 26 System.out.println(""); 27 28 } 29 30 31 } 32 33 }
hdu 1042
1 //package 大数; 2 3 import java.math.BigInteger; 4 import java.util.Scanner; 5 6 7 public class Main { 8 9 public static void main(String[] args) { 10 BigInteger ans,i,n,one; 11 Scanner sc=new Scanner(System.in); 12 while(sc.hasNextBigInteger()) 13 { 14 n=sc.nextBigInteger(); 15 one=new BigInteger("1"); 16 ans=new BigInteger("1"); 17 i=new BigInteger("1"); 18 19 while(i.compareTo(n)<=0) 20 { 21 22 23 ans=ans.multiply(i); 24 25 i=i.add(one); 26 27 28 } 29 System.out.println(ans); 30 31 } 32 } 33 34 }