NYOJ 773  开方数

http://acm.nyist.net/JudgeOnline/problem.php?pid=773

 1 import java.util.Scanner;
 2 
 3 public class Main{
 4     public static void main(String[] args){
 5         int n;
 6         double p;
 7         Scanner cin = new Scanner(System.in);
 8         while(true){
 9             n = cin.nextInt();
10             p = cin.nextDouble();
11             if(n == 0 && p == 0) break;
12             System.out.printf("%.0f\n", Math.pow(p, 1.0/n));
13         }
14     }
15 }
View Code

 

POJ 1001 Exponentiation

http://poj.org/problem?id=1001

 1 import java.io.*;
 2 import java.math.*;
 3 import java.util.*;
 4 import java.text.*;
 5 
 6 public class Main 
 7 {
 8     public static void main(String[] args) 
 9     {
10         Scanner cin = new Scanner(System.in);
11         while(cin.hasNext())
12         {
13        BigDecimal R = cin.nextBigDecimal();
14        int n = cin.nextInt();
15          String ans = R.pow(n).stripTrailingZeros().toPlainString();
16          if(ans.startsWith("0"))
17              ans = ans.substring(1);
18          System.out.println(ans);
19        }
20     }
21 }
View Code

 

HDU 1042  N!

http://acm.hdu.edu.cn/showproblem.php?pid=1042

java版:

 1 3
 2 4
 3 5
 4 6
 5 7
 6 8
 7 9
 8 10
 9 11
10 12
11 13
12 14
13 15
14 16
15 17
16 18
17 import java.util.Scanner;
18 import java.math.BigInteger;
19 public class Main{
20     public static void main(String[] args){
21         Scanner input = new Scanner(System.in);
22         int A, i;
23         while(input.hasNext())
24         {    
25             A = input.nextInt();
26             BigInteger B = BigInteger.ONE;
27             for(i=1; i<=A; i++)
28             {
29                 B = B.multiply(BigInteger.valueOf(i));
30             }
31             System.out.println(B);
32         }
33     }
34 }
View Code

C++版:

 1 3
 2 4
 3 5
 4 6
 5 7
 6 8
 7 9
 8 10
 9 11
10 12
11 13
12 14
13 15
14 16
15 17
16 18
17 19
18 20
19 21
20 22
21 23
22 24
23 25
24 26
25 27
26 28
27 29
28 30
29 31
30 32
31 33
32 34
33 35
34 36
35 37
36 38
37 39
38 40
39 41
40 42
41 43
42 44
43 45
44 46
45 47
46 48
47 49
48 #include<iostream>
49 #include<string.h>
50 #include<stdio.h>
51 #include<ctype.h>
52 #include<algorithm>
53 #include<stack>
54 #include<queue>
55 #include<set>
56 #include<map>
57 #include<math.h>
58 #include<vector>
59 #include<deque>
60 #include<list>
61 #define INF 0x7fffffff
62 #define inf 0x3f3f3f3f
63 #define maxn 40000
64 using namespace std;
65 int a[maxn];
66 int main()
67 {
68     int n;
69     int s;
70     while(scanf("%d",&n)!=EOF)
71     {
72         memset(a,0,sizeof(a));
73         a[0]=1;
74         for(int i=2; i<=n; i++)
75         {
76             int c=0;
77             for(int j=0; j<maxn; j++)
78             {
79                 s=a[j]*i+c;
80                 a[j]=s%10;
81                 c=s/10;
82             }
83         }
84         int p;
85         for(int i=maxn; i>=0; i--)
86             if(a[i])
87             {
88                 p=i;
89                 break;
90             }
91         for(int i=p; i>=0; i--)
92             printf("%d",a[i]);
93         printf("\n");
94     }
95     return 0;
96 }
View Code

 

 

FZOJ 1602  Best results

http://acm.fzu.edu.cn/problem.php?pid=1602

 1 import java.io.*;
 2 import java.math.*;
 3 import java.util.*;
 4 import java.text.*;
 5 
 6 public class Main 
 7 {
 8     public static void main(String[] args) 
 9     {
10         Scanner cin = new Scanner(System.in);
11         while(cin.hasNext())
12         {
13           int t = cin.nextInt();
14           BigDecimal ans = BigDecimal.ONE;
15           while(t-- > 0)
16             {
17            String str = cin.next();
18            int l = str.length();
19            str = str.substring(0, l - 1);
20            int tep = Integer.parseInt(str);
21           BigDecimal Tep = BigDecimal.valueOf(tep);
22            Tep = Tep.divide(BigDecimal.valueOf(100));
23            ans = ans.multiply(Tep);
24             }
25           System.out.println(ans.stripTrailingZeros().toPlainString());
26         }
27     }
28 }
View Code

 

 

ZOJ  3549   Little Keng

一开始超时了,百撕不得骑姐,后来意识到可能JAVA计算大数和小数的时候,复杂度还是差别挺大的,虽然理论上都是O(1)的操作,但是当数的规模增长到一定级数,运算效率的差别就显露出来了。

不超时的关键就是快速幂的时候加上取模的操作。

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3549

 1 import java.math.*;
 2 import java.util.*;
 3 
 4 public class Main {
 5     static BigInteger power(BigInteger p,BigInteger n,BigInteger m)            //快速幂
 6     {
 7         BigInteger sq=BigInteger.ONE;
 8         while(n.compareTo(BigInteger.ZERO)>0)                    //while(n>0)
 9         {
10             if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ONE)==0)    //if(n%2==1)
11                 sq=sq.multiply(p).mod(m);                //    sq=(sq*p)%m;
12             p=p.multiply(p).mod(m);                        //p=(p*p)%m;
13             n=n.divide(BigInteger.valueOf(2));                //n=n/2;
14         }
15         return sq.mod(m);
16     }
17     static boolean judge(int m,int n,int k)
18     {
19         BigInteger mod=BigInteger.ONE,ans=BigInteger.ZERO;
20         int i;
21         for(i=1;i<=k;i++)                            //10^k
22             mod=mod.multiply(BigInteger.valueOf(10));
23         for(i=1;i<=m;i++)                            //1^n+2^n+3^n+...+m^n
24         {
25             BigInteger a,b;
26             a=BigInteger.valueOf(i);
27             b=BigInteger.valueOf(n);
28             ans=(ans.add(power(a,b,mod))).mod(mod);
29         }
30         if(ans.mod(mod).compareTo(BigInteger.ZERO)==0) return true;
31         else return false;
32     }
33     public static void main(String args[])
34     {
35         Scanner cin=new Scanner(System.in);
36         int i,m,n;
37         while(cin.hasNext())
38         {
39             m=cin.nextInt();
40             n=cin.nextInt();
41             for(i=1;;i++)
42             {
43                 if(judge(m,n,i)) continue;
44                 else break;
45             }
46             System.out.println(i-1);
47         }
48     }
49 }
View Code

这是正解。。。

 

 1 import java.io.*;
 2 import java.math.*;
 3 import java.util.*;
 4 import java.text.*;
 5 
 6 class Main 
 7 {
 8    /* static BigInteger Pow(BigInteger a, int b)
 9     {
10         BigInteger ans = BigInteger.ONE;
11         while(b > 0)
12         {
13             if(b % 2 == 1)
14                 ans = ans.multiply(a);
15             ans = ans.multiply(ans);
16             b >>= 1;
17             //b /= 2; 
18         }
19         return ans;
20     } */
21 
22 
23      static BigInteger power(BigInteger p,BigInteger n)         //快速幂  
24     {  
25         BigInteger sq=BigInteger.ONE;  
26         while(n.compareTo(BigInteger.ZERO)>0)                    //while(n>0)  
27         {  
28             if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ONE)==0)   //if(n%2==1)  
29                 sq=sq.multiply(p);               //  sq=(sq*p)%m;  
30             p=p.multiply(p);                     //p=(p*p)%m;  
31             n=n.divide(BigInteger.valueOf(2));              //n=n/2;  
32         }  
33         return sq;  
34     } 
35 
36     public static void main(String[] args) 
37     {
38         Scanner cin = new Scanner (System.in);
39         while(cin.hasNext())
40         {
41             int m = cin.nextInt();
42             int n = cin.nextInt();
43 
44             //System.out.println(Pow(BigInteger.TEN, 1));
45 
46             BigInteger ans = BigInteger.ZERO;
47             for(int i = 1; i <= m; ++i)
48             {
49                 BigInteger I = BigInteger.valueOf(i);
50                 BigInteger N = BigInteger.valueOf(n);
51                 ans = ans.add(power(I, N));
52               //ans = ans.add(Pow(I, n));
53             }
54             int cnt = 0;
55             while(ans.mod(BigInteger.TEN).compareTo(BigInteger.ZERO) == 0)
56             {
57                 ans = ans.divide(BigInteger.TEN);
58                 ++cnt;
59             }
60                 
61           System.out.println(cnt);   
62         }
63         //System.out.println("Hello World!");
64     }
65 }
View Code

这是TLE的代码。。。

posted on 2015-04-06 22:12  wkxnk  阅读(221)  评论(0编辑  收藏  举报