UVa:10519 - !! Really Strange !!
来源:互联网 发布:百胜erp软件 费用 编辑:程序博客网 时间:2024/05/21 12:55
数据量这么大一看就是有公式,而且肯定要用大数
ans=n*(n-1)+2
用到了大数乘法,减法,加法。
要特别注意乘法开的数组要求是最大的乘数的两倍以上。
#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>#define MAXN 205using namespace std;void BigAdd(char *a,char *b,char *back){ char c[MAXN]; int p=0,up=0; for(int i=strlen(a)-1,j=strlen(b)-1; i>=0||j>=0;--i,--j) { int x,y,z; if(i<0) x=0; else x=a[i]-'0'; if(j<0) y=0; else y=b[j]-'0'; z=x+y+up; if(z>9) up=1; else up=0; c[p++]=z%10+'0'; } if(up) c[p++]='1'; c[p]=0; int q=0; for(int i=p-1; i>=0; --i) back[q++]=c[i]; back[q]=0;}void BigSub(char *a,char*b,char*back){ char c[MAXN]; int down=0,p=0; for(int i=strlen(a)-1,j=strlen(b)-1; i>=0; --i,--j) { int x,y,z; x=a[i]-'0'; if(j<0) y=0; else y=b[j]-'0'; z=x-y+down; if(z<0) { down=-1; z+=10; } else down=0; c[p++]=z+'0'; } c[p]=0; int q=0; bool ok=false; for(int i=p-1; i>=0; --i) { if(c[i]!='0') ok=true; if(ok) back[q++]=c[i]; } back[q]=0;}void BigMult(char *a,char *b,char *back){ int res[MAXN]= {0}; for(int i=strlen(a)-1,I=0; i>=0; --i,++I) for(int j=strlen(b)-1,J=0; j>=0; --j,++J) res[I+J]+=(a[i]-'0')*(b[j]-'0'); int L=strlen(a)+strlen(b)+1; for(int i=0; i<=L; ++i) { res[i+1]+=res[i]/10; res[i]=res[i]%10; } int p=0; bool ok=false; for(int i=L; i>=0; --i) { if(res[i]) ok=true; if(ok) back[p++]=res[i]+'0'; } if(!ok) back[p++]='0'; back[p]=0;}int main(){ char str[MAXN]; while(scanf("%s",str)!=EOF) { if(!strcmp(str,"0")) { puts("1"); continue; } char a[MAXN],b[MAXN],c[MAXN]; BigSub(str,"1",a); BigMult(str,a,b); BigAdd(b,"2",c); puts(c); } return 0;}
- UVA 10519 !! Really Strange !!
- UVA 10519 !! Really Strange !!
- UVa:10519 - !! Really Strange !!
- UVa 10519 - !! Really Strange !!
- UVA 10519 !! Really Strange !!
- UVa 10519 - !! Really Strange !!
- uva 10519!! Really Strange !!
- uva 10519 - !! Really Strange !!(规律)
- UVA 10519 - !! Really Strange !!(数论,规律)
- UVa 10519 !! Really Strange !! (递推)
- Really Strange!!
- !! Really Strange !!
- UVa10519 - !! Really Strange !!
- UVA10519 - !! Really Strange !!(数论+高精度)
- UVA 11529 Strange Tax Calculation
- UVA - 11529 Strange Tax Calculation
- UVA 11529 Strange Tax Calculation
- UVA 11529 - Strange Tax Calculation(计数问题)
- [转]Visual Studio 单元测试之五---数据库测试
- ARM处理器架构------可嵌套中断的实现
- AutoLayout(自动布局)入门
- xml json format
- 编程面试的10大算法概念汇总
- UVa:10519 - !! Really Strange !!
- web.xml
- [转]Visual Studio 单元测试之六---UI界面测试
- 获取网卡硬件地址代码
- 剑指Offer之和为S的连续正数序列
- struts.xml
- CentOS下sqoop的配置安装(整理篇)
- linux信号通信
- ibm云时代的转型