HDU 1042 大数计算
这道题一开始就采用将一万个解的表打好的话,虽然时间效率比较高,但是内存占用太大,就MLE
这里写好大数后,每次输入一个n,然后再老老实实一个个求阶层就好
java代码:
/**
* @(#)Main.java
*
*
* @author
* @version 1.00 2014/12/21
*/
import java.util.*;
import java.math.*; public class Main {
//public static BigInteger a [] = new BigInteger[10001];
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int n;
while(input.hasNext()){
n = input.nextInt();
BigInteger a [] = new BigInteger[2];
a[0] = new BigInteger("1");
for(int i = 1; i<=n ; i++){
String s = Integer.toString(i);
a[i&1] = new BigInteger(s);
// a[i].valueOf(i);
a[i&1] = a[i&1].multiply(a[1-(i&1)]);
} System.out.println(a[n&1]);
}
} }
c++代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ; typedef long long LL ; #define rep( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define For( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define rev( i , a , b ) for ( int i = a ; i >= b ; -- i ) const int M = ; struct BigInt {
int digit[] ;
int length ; BigInt () {
length = ;
memset ( digit , , sizeof digit ) ;
} BigInt ( LL number ) {
length = ;
memset ( digit , , sizeof digit ) ;
while ( number ) {
digit[length ++] = number % M ;
number /= M ;
}
} int operator [] ( const int index ) const {
return digit[index] ;
} int& operator [] ( const int index ) {
return digit[index] ;
} BigInt fix () {
while ( length && digit[length - ] == ) -- length ;
return *this ;
} BigInt operator + ( const BigInt& a ) const {
BigInt c ;
c.length = max ( length , a.length ) + ;
int add = ;
rep ( i , , c.length ) {
add += a[i] + digit[i] ;
c[i] = add % M ;
add /= M ;
}
return c.fix () ;
} BigInt operator - ( const BigInt& a ) const {
BigInt c ;
c.length = max ( a.length , length ) ;
int del = ;
rep ( i , , c.length ) {
del += digit[i] - a[i] ;
c[i] = del ;
del = ;
if ( c[i] < ) {
int tmp = ( c[i] - ) / M + ;
c[i] += tmp * M ;
del -= tmp ;
}
}
return c.fix () ;
} BigInt operator * ( const BigInt& a ) const {
BigInt c ;
c.length = a.length + length ;
rep ( i , , length ) {
int mul = ;
For ( j , , a.length ) {
mul += digit[i] * a[j] + c[i + j] ;
c[i + j] = mul % M ;
mul /= M ;
}
}
return c.fix () ;
} void show () {
printf ( "%d" , digit[length - ] ) ;
rev ( i , length - , ) printf ( "%04d" , digit[i] ) ;
printf ( "\n" ) ;
} } ; int main ()
{
int n;
while (~scanf("%d" , &n)) {
BigInt big[];
big[] = BigInt();
for(int i = ; i<=n ; i++)
big[i&] = big[-(i&)] * BigInt(i);
big[n&].show();
}
return ;
}
HDU 1042 大数计算的更多相关文章
- HDU 1042 大数阶乘
B - 2 Time Limit:5000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- Java & C++ 大数计算
Java--大数计算,妈妈再也不用担心我的学习了 . BigInteger 英文API: http://docs.oracle.com/javase/8/docs/api/ 中文API: http:/ ...
- HDU 1042 N! 參考代码
HDU 1042 N! 题意:给定整数N(0 ≤ N ≤ 10000), 求 N! (题目链接) #include <iostream> using namespace std; //每一 ...
- js 大数计算
js 大数计算 原理 JavaScript 安全整数 是 -253-1 ~ 253-1 ,即: -9007199254740991 ~ 9007199254740991; 换句话说,整数超过这个范围就 ...
- hdu 1042 N!(大数)
题意:求n!(0 ≤ N ≤ 10000) 思路:大数,用数组存储 1.首先要考虑数据N!的位数,因为最大是10000!,可以计算一下大概是5+9000*4+900*3+90*2+10*1=38865 ...
- HDU 1042 N!(高精度阶乘、大数乘法)
N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submi ...
- HDU 1131 Count the Trees 大数计算
题目是说给出一个数字,然后以1到这个数为序号当做二叉树的结点,问总共有几种组成二叉树的方式.这个题就是用卡特兰数算出个数,然后因为有编号,不同的编号对应不同的方式,所以结果是卡特兰数乘这个数的阶乘种方 ...
- hdu 1042 N!(大数的阶乘)
N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- hdu 1042 N! java大数及判断文件末尾
N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submi ...
随机推荐
- Python基础 — Pandas
Pandas -- 简介 Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的. Pandas ...
- 观光公交 2011年NOIP全国联赛提高组(贪心,递推)
观光公交 2011年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 风景迷人的小城 Y 市 ...
- 聊聊LuaJIT
JIT 什么是JITJIT = Just In Time即时编译,是动态编译的一种形式,是一种优化虚拟机运行的技术. 程序运行通常有两种方式,一种是静态编译,一种是动态解释,即时编译混合了这二者.Ja ...
- 洛谷 P3372 【模板】线段树 加法
题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个 ...
- BFS POJ 3414 Pots
题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- 内联标签------------大多数XHTML可以表示为两种类型的标签:块标签(block tag)和内联标签(inline tag)
内联标签 <em> 强调,大部分浏览器渲染为斜体. <strong> 强调,大部分浏览器渲染为黑体. <sub> 下标 <sup> 上标 内联标签通常用 ...
- IIS网站部署步骤以及常见异常解决方案
一. 简述 如果VS调试代码每次都使用浏览器打开,修改脚本和样式等还可以刷新页面显示最新修改,但是修改后台代码的话就需要停止调试再重新使用浏览器打开才能显示后台的最新修改,就比较麻烦.这里推荐附加到I ...
- 2017-11-28 Html-浅谈如何正确给table加边框
一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...
- Ionic2/angularJs2中的静态类 PhotoLibrary 调用不上
photoLibrary调用报错:No provider for PhotoLibrary: 在调用相册文件时有用到photolibrary,总有些莫名的报错,3月份的时候这个坑让我不知所措,现在写下 ...