【转】JAVA大数
在用C或者C++处理大数时感觉非常麻烦,但是在JAVA中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,
两个类的对象能表示最大范围理论上能够表示无线大的数,只要计算机内存足够大。 这两个类都在java.math.*包中,因此每次必须在开头处引用该包。
Ⅰ基本函数:
1.valueOf(parament); 将参数转换为制定的类型
比如 int a=3;
BigInteger b=BigInteger.valueOf(a);
则b=3;
String s=”12345”;
BigInteger c=BigInteger.valueOf(s);
则c=12345;
2.add(); 大整数相加
BigInteger a=new BigInteger(“23”);
BigInteger b=new BigInteger(“34”);
a.add(b);
3.subtract(); 相减
4.multiply(); 相乘
5.divide(); 相除取整
6.remainder(); 取余
7.pow(); a.pow(b)=a^b
8.gcd(); 最大公约数
9.abs(); 绝对值
10.negate(); 取反数
11.mod(); a.mod(b)=a%b=a.remainder(b);
12.max(); min();
13.public int comareTo();
14.boolean equals(); 是否相等
15.BigInteger构造函数:
一般用到以下两种:
BigInteger(String val);
将指定字符串转换为十进制表示形式;
BigInteger(String val,int radix);
将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger
Ⅱ.基本常量:
A=BigInteger.ONE 1
B=BigInteger.TEN 10
C=BigInteger.ZERO 0
Ⅲ.基本操作
1. 读入:
用Scanner类定义对象进行控制台读入,Scanner类在java.util.*包中
Scanner cin=new Scanner(System.in);// 读入
while(cin.hasNext()) //等同于!=EOF
{
int n;
BigInteger m;
n=cin.nextInt(); //读入一个int;
m=cin.nextBigInteger();//读入一个BigInteger;
System.out.print(m.toString());
}
Ⅳ.运用
四则预算:
import java.util.Scanner;import java.math.*;import java.text.*;public class Main {public static void main(String args[]){ Scanner cin = new Scanner ( System.in ); BigInteger a,b; int c; char op; String s; while(cin.hasNext() ) { a = cin.nextBigInteger(); s = cin.next(); op = s.charAt(0); if( op == '+') { b = cin.nextBigInteger(); System.out.println(a.add(b)); } else if( op == '-') { b = cin.nextBigInteger(); System.out.println(a.subtract(b)); } else if( op == '*') { b = cin.nextBigInteger(); System.out.println(a.multiply(b)); } else { BigDecimal a1,b1,eps; String s1,s2,temp; s1 = a.toString(); a1 = new BigDecimal(s1); b = cin.nextBigInteger(); s2 = b.toString(); b1 = new BigDecimal(s2); c = cin.nextInt(); eps = a1.divide(b1,c,4); System.out.print( a.divide(b) + " " + a.mod(b) + " "); if( c != 0) { temp = "0."; for(int i = 0; i < c; i ++) temp += "0"; DecimalFormat gd = new DecimalFormat(temp); System.out.println(gd.format(eps)); } else System.out.println(eps); } } }}例子:求一个n元素集合中k个元素的组合数量:C(n,k) = n!/(k! * (n-k)!)import java.math.BigInteger; import java.util.*;import java.io.*; public class FactorialBigIntegerTest { public static void main(String[] args) throws Exception { Scanner cin=new Scanner(System.in); int T = cin.nextInt(); for(int i = 0;i < T;i++){ int n=cin.nextInt(),k=cin.nextInt(); BigInteger nf= new BigInteger(""+factorial(n)); BigInteger kf= new BigInteger(""+factorial(k)); BigInteger nk = new BigInteger(""+factorial(n-k)); BigInteger n1 = new BigInteger(""+nf.divide(kf)); System.out.println(n1.divide(nk)); } } private static final BigInteger factorial(int n){ BigInteger bigInteger=new BigInteger(""+n); if(n==0) return BigInteger.ONE; else return factorial(n-1).multiply(bigInteger); } } 输入 2000 50运行结果:19961695734279236211032903597262781251004330223561192760088208846391036329722665786642280715824928160
例子:
poj2305
大数的进制转换
String st = Integer.toString(num, base); // 把num当做10进制的数转成base进制的st(base <= 35).
int num = Integer.parseInt(st, base); // 把st当做base进制,转成10进制的int(parseInt有两个参数,第一个为要转的字符串,
// 第二个为说明是什么进制).
BigInter m = new BigInteger(st, base); // st是字符串,base是st的进制.
1.如果要将一个大数以2进制形式读入 可以使用cin.nextBigInteger(2);
当然也可以使用其他进制方式读入;
2.如果要将一个大数转换成其他进制形式的字符串 使用cin.toString(2);//将它转换成2进制表示的字符串
关键代码:
Scanner cin=new Scanner(System.in);BigInteger p,m,ans;
p=cin.nextBigInteger(b);//从命令行以b进制读大数m=cin.nextBigInteger(b);ans=p.mod(m);//p%m
【转】调整phpmyadmin中的Cookie会话过期时间1440
phpmyadmin在使用过程中经常出现“登陆超时(1440秒未活动),请重新登录”,很烦
解决方法如下:
修改php.ini,找到
session.gc_maxlifetime = 1440
将数值改大就行了,然后使之生效
试验了一下,结果不好使。
最终解决方案:
找到 phpMyAdmin / libraries / config.default.php 文件,打开,修改
$cfg['LoginCookieValidity'] = 1440;
将1440修改成更大的值即可。
注意:$cfg['LoginCookieValidity']的值不能大于php.ini里的session.gc_maxlifetime的值,否则phpmyadmin 里会出现“您的 PHP 配置参数 session.gc_maxlifetime (外链,英文) 短于您在 phpMyAdmin 中设置的 Cookies 有效期,因此您的登录会话有效期将会比您在 phpMyAdmin 中设置的时间要更短。”错误。
ps:我在3.4.2和3.3.3下都修改成功,3.4.2的设置,功能里面能可以直接设置,但是我的php.ini里面最大设了1440,所以设得再大也只能本次会话有效
期待暑假爆发
放暑假就是我进入ACM队一周年的纪念日吧。
不知道今年能不能继续参加暑期集训,长期打酱油,怕被教练开掉啊。
不论如何,我不会放弃的。
最近快期末了,想多花点时间在学习上。
本以为这个赛季的现场赛都结束了,昨天接到njupt校赛邀请,我们学校去十几个人,我也报名了。
多参加现场赛,锻炼肯定比在宿舍里大得多。
平时得我太浮躁,很难静下心来思考困难的问题。
现场比赛倒是能够给我设定这样一个环境的,没有手机、因特网的干扰,我发现自己还是能安静想问题的。
只是,我太弱了,什么算法都不会,写模拟题的能力也不行,哎。。。
下午南邮网络赛,完全被虐啦。
一开始还不错,不到一个小时就实现了2题,rank里面还算靠前。
然后,突然就卡住了,好几道题都卡到爆,555。
直到最后一个小时才连出两题,最终以4题饮恨。
其实,这个比赛难度不大,山鬼、宇娘经5个小时的鏖战,都出了8题,偶尔卡一下,时间也不长,很均匀地就逐渐跑到rank的前两位。后面的wavwind,zlly,bearjie,都是6、7题,时间足的话也能再出题,可是4题的我和他们的差距太大了。。。不是很容易就能赶上的。
最后时刻,我围观宇娘,拍代码的速度,正确率,甚至贴测试数据的动作都比我快了许多。
差距是全方位的,我要虚心,利用暑假,好好学习。
不要再那么得浮躁了!!!
ACRush Topcoder问答语录
FameofLight ACRush: Finally the god of programming arrives
FameofLight ACRush: do you follow a random practice , or you practice question by topic , in your early days
ACRush FameofLight: No, our team practiced 3 or 4 full contests a week.
pt1989 ACRush: Can u tell us whom or what do u credit for ur success?
ACRush pt1989: I think more practice is the way.
ktuan ACRush: how can you arrange your study with 3-4 contests a week ?
ACRush ktuan: UVA, SGU , ZJU and PKU is enough.
pt1989 ACRush: what is ur method of practising? random or organised?
ACRush pt1989: contest env. We always solve programs in contest env.
Sunny_05 ACRush: wat is ur daily routine ?? i mean hw do u practice everyday ??
ACRush Sunny_05: We only have weekly routine for practice.
binarywithme ACRush: what u think about world final problems..level of difficulty
ACRush binarywithme: A little harder than 1000p.
MB__ ACRush: Which of these do you do after you submit your solution on TC: test solution, re-read problem statement, check code line by line
ACRush MB__: test for at least 10 cases.
martins256 ACRush: how many problems have you solved on OJ ?
ACRush martins256: about 2000.
kcm1700 ACRush: How many problems have you proposed on OJ or anything like this competition or something...?
ACRush kcm1700: about 2000.
vijay_comc ACRush: Who is your Arch Rival in Programming ? ;)
ACRush vijay_comc: Petr, I think.
SomethingWrong ACRush: What is your favourite Online Judge? :)
ACRush SomethingWrong: SGU.
vexorian ACRush: Why SGU?
ACRush vexorian: More tricky testcases.
Dee2306 ACRush: Being one of top programmers, do u believe its just practice which makes u perfect.. or some of u are born genius ?? :)
ACRush Dee2306: It's at least 80% due to practice
stormsky ACRush: how to practice ACM for a ACM beginner?
ACRush stormsky: practice in contest env.
piva ACRush: Do you train more of one category of problems? Or you just solve problems at random?
ACRush piva: I do more practice in the ones I have troubles with.
pt1989 ACRush: what are ur interests other than programming?
ACRush pt1989: WarCraft.
vijay_comc ACRush: Petr is already married. No plans to compete him in that area ? :D
ACRush vijay_comc: ....not yet.
Sarkin ACRush: Is analyzing algorithms an essential part of learning algorithms?
ACRush Sarkin: Not all, I think coding is in practice room is the rest way.
abdessamad ACRush: can believe it! when i see your challengers :D :D
ACRush abdessamad: It's one thing that Petr did much better than me.
stjepan ACRush: where did/do you practice most and how?
ACRush stjepan: And join contest.
binarywithme ACRush: why u choose c++ as a default programming language
ACRush binarywithme: er.. Stl is the first reason. Another one may be efficient.
geekru2 ACRush: during contest, what kind of problems do u enjoy doing? as in type of problem?
ACRush geekru2: dp and network-flow.
reginachris ACRush: What's the best OJ to start with for practice (UVa, SPOJ, TC, etc.)?
ACRush reginachris: USACO
ferry ACRush: What do you do when you can't solve a problem or you don't know what's wrong?
ACRush ferry: I will try to pass it in practice room. I always do that.
geekru2 ACRush: Do you solve puzzles and mind bending questions to improve your problem solving techniques?
ACRush geekru2: no. I don't like such problems like suduko.
Sarkin ACRush: What algorithm types helped you in the IOI?
ACRush Sarkin: dp.
abdessamad ACRush: did you like chess?
ACRush abdessamad: yes.
geekru2 ACRush: when in a team event(IOI etc), do you dominate while programming?
ACRush geekru2: yes. I guess.
kcm1700 ACRush: how do you prepare data for the challenge phase?
ACRush kcm1700: The trickiest one, of course.
binarywithme ACRush: sir what is the best way to learn DP.
ACRush binarywithme: TC contest.
[dasha] ACRush: When u were beginning to compete,did u have any problems? Like low speed of sloving, or maybe some particular method u couldn't understend for a long time, sth else? If yes, how did u get over that?
ACRush [dasha]: USACO is a really good place. Especially for the beginners.
frank44 ACRush: how long did the usaco practice take you?
ACRush frank44: half one year.
binarywithme ACRush: what u think math should b strong to become a Gud programmer
ACRush binarywithme: The mathematical foundation is helps.
khanhptnk ACRush: excuse me, do you think what we should prepare before an important contest ?
ACRush khanhptnk: relax, and rush some simple problems. And solve some problems in a contest env. is also helpful.
Sarkin ACRush: Do you recommned reading "Introduction to Algorithms"? if you know?
ACRush Sarkin: Really good.
coder29 ACRush: which parts do u advice to read in "Introduction to Algorithms"?
ACRush coder29: All parts are perfect. except complexity.
MB__ ACRush: do you do any sports but topcoding?
ACRush MB__: soccer.
vijay_comc ACRush: Complexity in CLRS is flawed ?? :o
ACRush vijay_comc: 7-8
Sarkin ACRush: What do you mean except comlexity?
ACRush Sarkin: That's only my opinion.
pcaldeira ACRush: which skill do you consider most important on TCHS/ioi-style contests?
ACRush pcaldeira: algorithm skills and coding ability.
coder29 ACRush: I am newbie...which OJ is better fr me...USCO or UVA?
ACRush coder29: USACO
hakami ACRush: Are you useing library or typing from first for SRMs?
ACRush hakami: some includes and basic untilities.
vrajesh1989 ACRush: Will you try to prove your algorithms to yourselves during contest time or just believe your intuition and start coding?
ACRush vrajesh1989: Yes, it's very important for me.
Sarkin ACRush: Do you use algorithm analyzis when solving a problem to see it's effeciency?
ACRush Sarkin: Yes. the effeciency sometimes is more important than correctness in TC contest.
coder29 ACRush: basically i want 2 to do pratice on DP and greedy types...is TCs' room are sufficient..?
ACRush coder29: A bulk of dp tasks in SRMs. TC's room is good.
rajeshsr ACRush: In short, what is the strategy for becoming a good coder, according to u?
ACRush rajeshsr: Practice is the way.
Lingmum ACRush: How can we improve our speed of solving the problems,more problems?
ACRush Lingmum: More practice.
stormsky ACRush: do you often practice TC?and how to practice?
ACRush stormsky: Yes. But in fact, I prefer to doing past Regionals and finals.
sahiltiwari ACRush: how many hours you practise per day ?
ACRush sahiltiwari: 4-5
MohammadReza ACRush: What do you do to make your code becomes bugfree although the big size?
ACRush MohammadReza: test more tricky cases.(corner cases)
rajeshsr ACRush: Sorry to be repetitious, But I want to know what is the strategy of practice you employed? U select some random problems from OJs and solve or try to master a particular domain like DP by solving problems based on that or any other thing?
ACRush rajeshsr: set problems of past regionals and finals.
puneetkp444 ACRush: Can you suggest some ways to improve problem solving abilities
ACRush puneetkp444: practice all kinds of problems.
alft7 ACRush: what do you usually do before a very important competition, practise or something else?
ACRush alft7: practice until 3 days before it.
ferry ACRush: What is the hardest problem you've done?
ACRush ferry: I in WF2007.
《程序员应该知道的97件事》读书笔记
英文:o'reilly
中文:博文视点 电子工业出版社
定价:45元,太贵,适合于从图书馆借来,每天睡觉前看一会儿。。。
下面是我的读书笔记,《短文标题,小标题》:
Beauty is in simplicity
1.阅读公认专家的编码
2.使用更短的方法 5-10行也不算极端
before your Refactor
1.起点是:清理已有的基础代码和基于这些代码写的测试
2.避开重写的诱惑(重新测试麻烦)
3.逐步增加小功能
4.新技术不是重构的理由
coding with reason
1.变量最小作用域
2.空行分割
3.需要内嵌的代码-》封装成函数
4.函数要短,古老的24行限制依然有用
5.函数参数四个以内(相关参数组合成局部对象)
6.封装且接口要窄
7.setter尽量少用
continuous learning
1.杂志、博客、微博
2.亲自编写
3.导师(最厉害的家伙)
4.看源码学习
5.午餐学习会、用户组、小范围讨论会(给别人讲授将会大大激发学习热情)
6.加入技术小组
7.参加讨论会,获得网上免费资料
8.使用播客,打发路上时间
9.《the pragmatic programmer》
do lot of deliberate practise
rt
未完待续。。。