HDU 4927 big data, hdu4927 Big Data
The question is simple:
For the number of n characters in length, perform n-1 times to generate a new series:
B1 = a2-a1 b2= a3-a2 b3 = a4-a3
C1 = b2-b1 c2 = b3-b2
Ans = c2-c1
Finally, the formula is as follows: Yang Hui triangle of n's row
For example:
3
1 2 3
Ans = 1*1-2*2 + 1*3 = 0
4
1 5 7 2
Ans =-1*1 + 3*5-3*7 + 1*2 =-5
You can optimize each number in the Yang Hui triangle. The last number can be multiplied and divided by the previous one.
JAVA uses the number of seconds... BUT not JAVA --#
Use C ++ to simulate large numbers ..
#include "stdio.h"#include "string.h"int a[3010];__int64 mark[3010],ans[3010],c[3010];int Max(int a,int b){ if (a<b) return b;else return a;}void make_mul(int x){ int i; for (i=1;i<=mark[0];i++) mark[i]*=x; for (i=1;i<=mark[0];i++) { mark[i+1]+=mark[i]/1000000; mark[i]%=1000000; } while (mark[mark[0]+1]!=0) { mark[0]++; mark[mark[0]+1]=mark[mark[0]]/1000000; mark[mark[0]]%=1000000; }}void make_div(int x){ int i; for (i=mark[0];i>=2;i--) { mark[i-1]+=(mark[i]%x)*1000000; mark[i]/=x; } mark[1]/=x; while (mark[mark[0]]==0) mark[0]--;}void make_add(){ int i,op; if (ans[0]>0) { for (i=1;i<=mark[0];i++) { ans[i]+=mark[i]; ans[i+1]+=ans[i]/1000000; ans[i]%=1000000; } while (ans[ans[0]+1]!=0) { ans[0]++; ans[ans[0]+1]=ans[ans[0]]/1000000; ans[ans[0]]%=1000000; } return ; } else { if (-ans[0]>mark[0]) op=-1; else if (-ans[0]<mark[0]) op=1; else { for (i=mark[0];i>=1;i--) if (mark[i]>ans[i]) { op=1;break;} else if (mark[i]<ans[i]) { op=-1; break;} if (i==0) { memset(ans,0,sizeof(ans)); ans[0]=1; return ; } } if (op==1) { for (i=1;i<=mark[0];i++) { ans[i]=mark[i]-ans[i]; if (ans[i]<0) { mark[i+1]--; ans[i]+=1000000; } } ans[0]=-ans[0]; while (ans[ans[0]]==0) ans[0]--; } else { for (i=1;i<=-ans[0];i++) { ans[i]=ans[i]-mark[i]; if (ans[i]<0) { ans[i+1]--; ans[i]+=1000000; } } while (ans[-ans[0]]==0) ans[0]++; } }}void make_red(){ int i,op; if (ans[0]<0) { for (i=1;i<=mark[0];i++) { ans[i]+=mark[i]; ans[i+1]+=ans[i]/1000000; ans[i]%=1000000; } while (ans[-ans[0]+1]!=0) { ans[0]--; ans[-ans[0]+1]=ans[-ans[0]]/1000000; ans[-ans[0]]%=1000000; } return ; } else { if (ans[0]>mark[0]) op=1; else if (ans[0]<mark[0]) op=-1; else { for (i=mark[0];i>=1;i--) if (mark[i]>ans[i]) { op=-1;break;} else if (mark[i]<ans[i]) { op=1; break;} if (i==0) { memset(ans,0,sizeof(ans)); ans[0]=1; return ; } } if (op==1) { for (i=1;i<=mark[0];i++) { ans[i]=ans[i]-mark[i]; if (ans[i]<0) { ans[i+1]--; ans[i]+=1000000; } } while (ans[ans[0]]==0) ans[0]--; } else { for (i=1;i<=mark[0];i++) { ans[i]=mark[i]-ans[i]; if (ans[i]<0) { mark[i+1]--; ans[i]+=1000000; } } ans[0]=mark[0]; while (ans[ans[0]]==0) ans[0]--; ans[0]=-ans[0]; } }}int main(){ int t,n,i,x,y,j,m,op; scanf("%d",&t); while (t--) { scanf("%d",&n); for (i=1;i<=n;i++) scanf("%d",&a[i]); memset(ans,0,sizeof(ans)); ans[0]=1; if (i==1) { printf("%d\n",a[1]); continue; } memset(mark,0,sizeof(mark)); mark[0]=1; mark[1]=1; if (n%2==1) ans[1]=a[1]; else ans[1]=-a[1]; x=1; y=n-1; for (i=2;i<=n;i++) { make_mul(y); y--; make_div(x); x++; memcpy(c,mark,sizeof(mark)); make_mul(a[i]); if (n%2==1) { if (i%2==1) make_add(); else make_red(); } else { if (i%2==0) make_add(); else make_red(); } memcpy(mark,c,sizeof(c)); } if (ans[0]>0) { printf("%I64d",ans[ans[0]]); for (i=ans[0]-1;i>=1;i--) printf("%06I64d",ans[i]); printf("\n"); } else { printf("-"); printf("%I64d",ans[-ans[0]]); for (i=-ans[0]-1;i>=1;i--) printf("%06I64d",ans[i]); printf("\n"); } } return 0;}
HDU 1066 takes the last place as the result of the big data factorial. Don't miss it when you pass.
How do I feel that lz is working on acm question timeout? The algorithm does not have any problems. How do you know that the numbers are big and you are wrong?
Acm Large Number Addition
The lz program is not just a local error.
The first main is not mian
The second j is not initialized and is a random number.
The output of the third printf should be % d.
There is also an algorithm problem. Why does the addition of large numbers in lz have only one addition?
Isn't there a carry-in problem in addition of one-bit or one-bit? Is it possible that lz considers this?
/*
* Objective: To solve Baidu's knowledge problem
* Time: 2010-04-14
* Author: XXX
*/
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Int I, len1, len2, len_min, len_max, flag = 0;
Char s1 [101]; // two integers
Char s2 [101];
Int sum [101] = {0}; // records and
Printf ("the first nunber \ n ");
Gets (s1); // obtain the addend
Printf ("the second nunber \ n ");
Gets (s2 );
Len1 = strlen (s1 );
Len2 = strlen (s2 );
Len_max = len1> len2? Len1: len2;
Len_min = len1> len2? Len2: len1;
For (I = 0; I <= len_max; I ++) // addition of computing trees
{
If (I <= len_min)
{
Sum [len_max-i] = s1 [len1-i]-'0' + s2 [len2-i]-'0' + flag ;;
If (sum [len_max-i]> 10) // process carry calculation, flag record whether there is an advance
{
Sum [len_max-i] = sum [len_max-i]-10;
Flag = 1;
}
Else
Flag = 0;
}
Else
{
If (len1> len2)
{
Sum [len_max-i] = s1 [len1-i]-'0' + flag;
If (sum [len_max-i]> 10) // process carry calculation, flag record whether there is an advance
{
Sum [len_max-i] = sum [len_max-i]-10;
Flag = 1;
}
Else
Flag = 0;
}
Else
{
Sum [len_max-i] = s2 [len2-i]-'0' + flag;
If (sum [len_max-i]> 10) // process carry calculation, flag record whether there is an advance
{
Sum [len_max-i] = sum [len_max-i]-10;
Flag = 1;
}
Else
Flag = 0;
}
}
}
If (flag = 1) // if the two digits are the same, if the input is output
Printf ("1 ");
For (I = 0; I <len_max; I ++)
Printf ("% d", sum [I]);
Return ...... remaining full text>