(部分转载,部分原创)java大数类(2)

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的代码。。。

转载于:https://www.cnblogs.com/qiucz/p/4396848.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/574678.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql 分类汇总_sql多级分类汇总实现介绍

t1id parentidm an ae mf mx fy fz bt2row id amount1 a 13.002 b 20.003 e 20.004 f 20.005 x 20.006 y 20.007 z 20.008 e 12.009 x 11.0010 f 13.00如何得出如下结果&#xff1a;row id amount7 x 20.0011 x 11.00x小计 31.008 y 20.00y小计 20.006 f 20.0012 f 13.00f小计 …

Altium Designer哪里下载和导入元件库_图文教程

http://jingyan.baidu.com/article/46650658064621f549e5f88f.html转载于:https://www.cnblogs.com/Ph-one/p/4397460.html

python3 logging com1_python-logging-基础(1)

在执行用例的时候&#xff0c;往往会遇到各种问题&#xff0c;遇到问题后很难去定位import loggingclass Logs:def __init__(self,filepath,name):#self.namename#self.filepathfilepathself.configRead_config(Config_Http)#重新定义日志名字self.loggerlogging.getLogger(sel…

拷贝人家的

TCP server端 #include "stdafx.h" #include <stdio.h> #include <winsock2.h>#pragma comment(lib,"ws2_32.lib")int main(int argc, char* argv[]) {//初始化WSAWORD sockVersion MAKEWORD(2,2);WSADATA wsaData;if(WSAStartup(sockVersion…

python问题解决方案_Python安装、遇到的问题及解决方案,python,和,方法

Python安装&#xff1a;先在官网下载你需要的Python版本&#xff0c;我这边下载的是Python3.8&#xff0c;下载完是一个exe文件&#xff0c;直接双击安装即可&#xff0c;注意勾选“add python 3.8 to path”安装后遇到的问题&#xff1a;1.安装完成后在cmd中输入Python查看版本…

python 复数求模_Python基础语法知识汇总(学习党的最爱!)

本文章包含了Python一系列基本知识&#xff0c;其中包括&#xff1a;基本数据类型&#xff08;整数&#xff0c;浮点数&#xff0c;复数&#xff0c;字符串&#xff09;&#xff1b;分支语句&#xff1b;异常处理&#xff1b;函数&#xff1b;局部变量与全局变量&#xff1b;递…

JAVA装mysql_已经安装了mysql,怎么能在java程序里使用SQL?

展开全部1、安装62616964757a686964616fe4b893e5b19e31333335323437SQLServer2000安装SQLServer2000补丁SP3安装SQLServer2000 for SP3的驱动程序(先打补丁sp3&#xff0c;再安装针对sp3的驱动程序&#xff0c;安装补丁时&#xff0c;为保险起见&#xff0c;两种验证方式的都装…

用对工具,抖音、某站视频轻松下载~

相信大部分小伙伴都有过这样的困扰&#xff0c;平时我们在刷短视频的时候就发现一段我们需要的视频。想下载时才发现&#xff0c;“保存”按钮是灰色的。 这个时候我们可以通过复制视频链接的方式来下载视频。下面给大家介绍四种万能视频下载工具&#xff0c;大家记得点赞收藏再…

数字的空洞 水 南邮NOJ 1071

数字的空洞 时间限制(普通/Java) : 1000 MS/ 3000 MS 运行内存限制 : 65536 KByte 总提交 : 209 测试通过 : 120 题目描述 在个位数中&#xff1a;0&#xff0c;4&#xff0c;6&#xff0c;8&#xff0c;9有一个共同的特征&#xff1a;数形上存在空洞&…

bzoj 1257: [CQOI2007]余数之和sum 数论

1257: [CQOI2007]余数之和sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id1257Description 给出正整数n和k&#xff0c;计算j(n, k)k mod 1 k mod 2 k mod 3 … k mod n的值&#xff0c;其中k mod i表示k除以i的…

flask 上传excel 前端_flask-restful编写上传图片api

Flask-RESTful是用于快速构建REST API的Flask扩展。我最近在使用Flask-Restful Vue.js写一个轻量博客时有一个前端后端上传图片的需求。在Flask-Restful的官方文档中并没有相关的内容。下面是我谷歌查找资料的总结。引入FileStorageflask-restful的参数解析中并没有文件类型&a…

Hibernate merge和update的区别

今天做了个测试&#xff0c;写了个测试用例来看看merge与update时控制台打印出来的日志有什么不一样。实体bean很简单&#xff0c;就id和name两个字段&#xff0c;接下来分别给出以下几种测试情形的控制台日志内容&#xff1a; 1. 数据库记录已存在&#xff0c;更改person的nam…

qdir 类似工具_qdir 类似工具_支持 Win8.1,全能资源管理器 Q-Dir 5.74 发布

Q-Dir是一款帮助用户管理本地文件和文档的工具。支持快速访问定位本地磁盘、网络驱动器、USB移动设备及其他存储设备中的文件或文档。依靠强大的Quadro-View技术&#xff0c;使得Q-Dir成为一款优秀的文件管理工具。Q-Dir文件管理软件特色&#xff1a;• 收藏夹&#xff1a;快速…

面试题:求所占字符

/* 在32位系统下&#xff0c;分别定义如下两个变量&#xff1a;char *p[10],char(*p1)[10],sizeof(p),sizeof(p1)分别值为___________。 */ #include <stdio.h> #include <stdlib.h>void main() {char *p[10],(*p1)[10],p2[10],p3;printf("%d,%d,%d,%d,%d,%d&…

mysql数据库函数转义函数_MySql数据库-查询、插入数据时转义函数的使用

最近在看一部php的基础视频教程&#xff0c;在做案例的时&#xff0c;当通过用户名查询用户信息的时候&#xff0c;先使用了转义函数对客户提交的内容进行过滤之后再交给sql语句进行后续的操作。虽然能看到转义函数本身的作用&#xff0c;但是仍然有一些疑惑。疑惑一&#xff1…

python pyquery安装_win7下python安装pyquery

安装pyquery之前首先要明确一点&#xff0c;easyinstall 是一款python包管理器&#xff0c;类似于node的npm&#xff0c;用于安装python的扩展包&#xff0c;它安装的包是以*.egg的方式。要安装pq需要经历以下步骤&#xff1a;1&#xff1a;下载easyinstall设置环境变量&#x…

DbContext 和ObjectContext两者的区别

一是ObjectContext是一种模型优先的开发模式&#xff0c;DbContext是代码优先的开发模式。这是两者最根本的区别。 同时两者之间可以相互转换&#xff1a; 下面给出转换的例子 1 DbContext转为ObjectContext using System.Data.Entity.Infrastructure ObjectContext context (…

mysql的简介与优点_SQL 简介以及MySQL的优点

原文摘自&#xff1a;W3school。还有一些是本人自己整理的SQL 是用于访问和处理数据库的标准的计算机语言。什么是SQL&#xff1f;SQL 指结构化查询语言SQL 使我们有能力访问数据库SQL 是一种ANSI的标准计算机语言注&#xff1a;ANSI&#xff0c;美国国家标准化组织SQL能做什么…

反射之关于MethodInfo的使用

1、MethodInfo类是在System.Reflection命名空间底下&#xff0c;既然是在Reflection空间底下。故名思议关于反射相关的操作&#xff0c;其中比较重要的方法是Invoke()方法&#xff0c;它是加载相同程序集的方法。简单用法 string command "AnnouncementSave"; //通过…

vue添加定位功能_Vue如何实现锚点定位功能?

整个链路应该是这样的&#xff0c;用户在消息中心中看到回复自己的信息&#xff0c;点进去后会跳到视频播放页面&#xff0c;页面url中会带上此次消息的replyId(就简称rid吧)&#xff0c;然后接下来的问题就是有两个&#xff0c;一个是评论本身是分页的&#xff0c;你的评论不一…