HDU5047 Sawtooth 大数乘法|数学递推 Java&&C
来源:互联网 发布:制造业 数据分析部门 编辑:程序博客网 时间:2024/05/21 08:52
题目链接:HDU5047
M型分割平面,经过推导,公式为F(n)=8*n*n-7*n+1,如果不懂,请看我的博客折线分割平面问题总结
因为这道题数据很大,得用大数。
所以这里提供JAVA和C的两种ACcode
C代码:代码参考自:http://blog.csdn.net/u012860063/article/details/39617811
/*2017年9月20日14:10:43HDU5047大数模板(C)+简单递推 AC 390ms*/#include <iostream>#include <map>#include <set>#include <string>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <queue>#include <vector> # include<string.h> # include<malloc.h> using namespace std;typedef long long ll;const int maxn=200;char t1[maxn],t2[maxn] ,t3[maxn],ans[maxn],s[maxn];char a[10],b[10],c[10];/*大数模板*/ void add(char* a,char* b,char* c) { int i,j,k,max,min,n,temp; char *s,*pmax,*pmin; max=strlen(a); min=strlen(b); if (max<min) { temp=max; max=min; min=temp; pmax=b; pmin=a; } else { pmax=a; pmin=b; } s=(char*)malloc(sizeof(char)*(max+1)); s[0]='0'; for (i=min-1,j=max-1,k=max;i>=0;i--,j--,k--) s[k]=pmin[i]-'0'+pmax[j]; for (;j>=0;j--,k--) s[k]=pmax[j]; for (i=max;i>=0;i--) if (s[i]>'9') { s[i]-=10; s[i-1]++; } if (s[0]=='0') { for (i=0;i<=max;i++) c[i-1]=s[i]; c[i-1]='\0'; } else { for (i=0;i<=max;i++) c[i]=s[i]; c[i]='\0'; } free(s); } /*大数减法*/ void subtract(char* a,char* b,char* c) { int i,j,ca,cb; ca=strlen(a); cb=strlen(b); if (ca>cb||(ca==cb&&strcmp(a,b)>=0)) { for (i=ca-1,j=cb-1;j>=0;i--,j--) a[i]-=(b[j]-'0'); for (i=ca-1;i>=0;i--) if (a[i]<'0') { a[i]+=10; a[i-1]--; } i=0; while (a[i]=='0') i++; if (a[i]=='\0') { c[0]='0'; c[1]='\0'; } else { for (j=0;a[i]!='\0';i++,j++) c[j]=a[i]; c[j]='\0'; } } else { for (i=ca-1,j=cb-1;i>=0;i--,j--) b[j]-=(a[i]-'0'); for (j=cb-1;j>=0;j--) if (b[j]<'0') { b[j]+=10; b[j-1]--; } j=0; while (b[j]=='0') j++; i=1; c[0]='-'; for (;b[j]!='\0';i++,j++) c[i]=b[j]; c[i]='\0'; } } /* 大数乘法*/ void multiply(char* a,char* b,char* c) { int i,j,ca,cb,* s; ca=strlen(a); cb=strlen(b); s=(int*)malloc(sizeof(int)*(ca+cb)); for (i=0;i<ca+cb;i++) s[i]=0; for (i=0;i<ca;i++) for (j=0;j<cb;j++) s[i+j+1]+=(a[i]-'0')*(b[j]-'0'); for (i=ca+cb-1;i>=0;i--) if (s[i]>=10) { s[i-1]+=s[i]/10; s[i]%=10; } i=0; while (s[i]==0) i++; for (j=0;i<ca+cb;i++,j++) c[j]=s[i]+'0'; c[j]='\0'; free(s); } /*大数除法,返回余数*/ int dividor(char* a,int b,char* c) { int i,j,temp=0,n; char* s; n=strlen(a); s=(char*)malloc(sizeof(char)*(n+1)); for (i=0;a[i]!=0;i++) { temp=temp*10+a[i]-'0'; s[i]=temp/b+'0'; temp%=b; } s[i]='\0'; for (i=0;s[i]=='0'&&s[i]!='\0';i++); if (s[i]=='\0') { c[0]='0'; c[1]='\0'; } else { for (j=0;s[i]!='\0';i++,j++) c[j]=s[i]; c[j]='\0'; } free(s); return temp; } int main(){a[0]='8';a[1]='\0';b[0]='7';b[1]='\0';c[0]='1';c[1]='\0';int t,kase=0;scanf("%d",&t);getchar(); while(t--){//memset(ans,'\0',sizeof(ans));gets(s);multiply(s,s,t1);multiply(t1,a,t2);multiply(s,b,t3);subtract(t2,t3,ans);add(ans,c,ans);printf("Case #%d: %s\n",++kase,ans);}}
/* * 2017年9月20日16:18:57 * HDU5047 * 大数 * AC * * 1154ms * */import java.io.*;import java.util.*;import java.math.*;public class Main {public static void main(String []args){//Scanner cin=new Scanner(System.in);/*下面这种写法比上面在这种写法快了500ms*/Scanner cin=new Scanner(new BufferedInputStream(System.in));PrintWriter cout=new PrintWriter(System.out);int t=cin.nextInt();int kase=0;while(t!=0){t--;kase++;BigInteger ans,tmp;BigInteger n=cin.nextBigInteger();ans=n.multiply(n);ans=ans.multiply(BigInteger.valueOf(8));tmp=n.multiply(BigInteger.valueOf(7));ans=ans.subtract(tmp);ans=ans.add(BigInteger.ONE);cout.println("Case #"+kase+": "+ans);//System.out.println("Case #"+kase+": "+ans);}cout.flush();}}
阅读全文
0 0
- HDU5047 Sawtooth 大数乘法|数学递推 Java&&C
- hdu5047 Sawtooth 规律 + JAVA大数
- hdu5047 Sawtooth(推导+java大数)
- HDU 5047 Sawtooth(大数优化+递推公式)
- HDU 5047 Sawtooth(数学 公式 大数)
- hdu5047(上海网赛1006)Sawtooth
- hdu5047(上海网赛1006)Sawtooth
- [Java大数+输入输出优化] hdu 5047 Sawtooth
- HDU 5047 Sawtooth (JAVA大数类)
- 大菲波数(递推 + JAVA大数)
- 大数乘法 C语言
- 大数乘法C实现
- 大数乘法(C++)
- C语言大数乘法
- 大数 乘法 C语言
- c语言大数乘法
- c语言-大数乘法
- 大数加减乘法(Java)
- Python 并行任务技巧
- codeforces 862A Mahmoud and Ehab and the MEX
- Maven默认jre更换为jdk
- 人工智能课程实验·过河问题/实验一 知识表示方法
- 负载均衡的几种基本算法和java代码实现
- HDU5047 Sawtooth 大数乘法|数学递推 Java&&C
- 统计学简介之八——一个总体参数的区间估计
- 分布式与集群的区别
- 序列(rms模拟7-1)* *(贪心)
- VS2010 MFC Dialog设置定时器
- 微信打开本地app
- Bomb HDU
- codeforces 847M Weather Tomorrow
- 商城HTML整站模版