题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047
PS:
推出公式:8*n^2 - 7*n + 1 ,套一下模板就好了! 不过很多大数模板这题是会T的,不过这个模板:http://blog.csdn.net/u012860063/article/details/39612037 多亏了队友的神模板2333333!
代码如下:
#include
#include
#include
/*大数加法*/
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=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=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