Show all threads Hide all threads Show all messages Hide all messages | An easy way to enlarge numbers | cold_Iron | 1012. K-based Numbers. Version 2 | 28 Apr 2005 22:17 | 1 | Anyone who speaks russian look here, there is an article about long arithmetic: http://comp-science.narod.ru/DL-AR/da.htmlI've written prog for this problem, but it was accepted to 1013 also. Thanks to Mr. Okulov! | I made a dimamic program, It should word, It worked for 1009 | Nazgur ( Ivan Nicolae ) | 1012. K-based Numbers. Version 2 | 23 Mar 2005 01:49 | 2 | My idea is simple: a[0]=1; a[1]=k-1; for i>=2 -> a[i]=(k-1)*a[i-1]+a[i-2]; It should work. Here is my code: var n,k,i:longint; a,b,c:extended; begin readln(n,k); a:=1; b:=k-1; for i:=2 to n do begin c:=(k-1)*(a+b); a:=b; b:=c; end; writeln(c:0:0); end. No, your 'dimamic program' shouldn't 'word' because you have to use big numbers... Edited by author 23.03.2005 06:42 | Why Access Violation? | ILTK! | 1012. K-based Numbers. Version 2 | 24 Jul 2004 17:54 | 2 | program d1013; {$APPTYPE CONSOLE} type longnum=array[1..3000] of longint; function getlen(num:longnum):longint; var i:longint; begin i:=3000; while (i>=1) do if (num[i]=0) then dec(i) else break; getlen:=i; end; function sum(num1,num2:longnum):longnum; var h,i,l1,l2,ost:longint; begin l1:=getlen(num1); l2:=getlen(num2); ost:=0; if l1>l2 then begin for i:=1 to l1 do begin h:=num1[i]; num1[i]:=(num1[i]+num2[i]+ost) mod 10; ost:=(h+num2[i]+ost) div 10; end; num1[l1+1]:=num1[l1+1]+ost; sum:=num1; end else begin for i:=1 to l2 do begin h:=num2[i]; num2[i]:=(num2[i]+num1[i]+ost) mod 10; ost:=(h+num1[i]+ost) div 10; end; num2[l2+1]:=num2[l2+1]+ost; sum:=num2; end; end; function mul(k:longint; num:longnum):longnum; var h,ost,l1,i:longint; begin l1:=getlen(num); ost:=0; for i:=1 to l1 do begin h:=num[i]; num[i]:=(num[i]*k+ost) mod 10; ost:=(h*k+ost) div 10; end; num[l1+1]:=ost; mul:=num; end; var n,k,i:longint; work1,work2,work3,work:longnum; begin readln(n,k); if (n<2) or (k<2) then halt; if k>10 then begin i:=n; n:=k; k:=i; end; { if k=1 then begin write(0); end;} fillchar(work1,sizeof(work1),0); fillchar(work2,sizeof(work2),0); fillchar(work3,sizeof(work3),0); work1[1]:=k mod 10; work1[2]:=k div 10; work2[1]:=((k-1)*(k+1))mod 10; work2[2]:=((k-1)*(k+1))div 10; for i:=3 to n-1 do begin work3:=mul((k-1),sum(work1,work2)); work1:=work2; work2:=work3; end; fillchar(work,sizeof(work),0); if n>3 then begin work:=mul(k-1,work3); end else if n=3 then work:=mul(k-1,work2) else work:=mul(k-1,work1); k:=getlen(work); for i:= k downto 1 do write(work[i]); end. Thank you for help. I understand my mistake. | How to be bigger? | Tang RZ | 1012. K-based Numbers. Version 2 | 2 Jun 2004 13:13 | 4 | I used int64, but it was still too small! How can it be bigger? You know long arithmetic? This problem solved usin it. Sorry, I don't know. Could you tell me? Sorry, but long arigthmetic very big section in programming. If you want, i can give you article about it(in Russian) or my program. Send me in this mail: marina_twins@mail.ru. Else you may find about it in Internet. | why I got wrong? | grey | 1012. K-based Numbers. Version 2 | 27 May 2004 18:56 | 2 | program fa; var r:longint; i,j,k,n:longint; function c(a,b:longint):longint; var i1,q:longint; begin if (b=0)or(a=b) then begin c:=1; exit; end; q:=1; for i1:=a downto a-b+1 do begin q:=q*i1 div (a-i1+1); end; c:=q; end; begin r:=0; read(n); read(k); for i:=0 to n div 2 do begin r:=r+(c(n-i,i))*trunc(exp((n-i)*ln(k-1))); end; writeln(r); end. Use long arithmetic and algorithm 1009 :))) | dynamic programming | Marcin Mika | 1012. K-based Numbers. Version 2 | 19 Apr 2004 06:34 | 3 | i solved the first K-Based problem with dynamic programming, but for K-Based Version 2 it's not fast enough. can someone please help me? #include <stdio.h> #include <string.h> FILE *in,*out; int ways[200]; int n,k; int get_ways( int nn ){ if ( nn==1 ) return k-1; if ( !nn ) return 1; if ( ways[nn]>-1 ) return ways[nn]; ways[nn]=0; for ( int i=1; i<k; i++ ) ways[nn]+=get_ways( nn-1 )+get_ways( nn-2 ); return ways[nn]; } void main( void ){ in=stdin; out=stdout; fscanf( in, "%d %d", &n, &k ); for ( int i=0; i<200; i++ ) ways[i]=-1; printf( "%d\n", get_ways( n ) ); } Just look at my page: acmsolver.timus.ru There is a description how to solve it. SAV u can use a cycle instead of recursion. and the change array (int)ways[200] into (int)ways[3][2000]; because the result will has maore than 100 digits, mathematic arithmetic is needed. i change you programme as follow: //dynamic programming #include <iostream.h> #include <stdio.h> int ways[3][20000]={0}; int ADD(int a[], int b[], int c[]); int MUL(int a[], int k); int main() { long n, k, i, j; cin>>n>>k; ways[0][1] = 1; ways[0][0] = 1; ways[1][1] = k-1; ways[1][0] = 1; for (i=2; i<=n; i++) { ADD(ways[2], ways[1], ways[0]); MUL(ways[2], k-1); j = 2; i++; if (i>n) break; ADD(ways[0], ways[2], ways[1]); MUL(ways[0], k-1); j = 0; i++; if (i>n) break; ADD(ways[1], ways[0], ways[2]); MUL(ways[1], k-1); j = 1; } for (i=ways[j][0]; i>=1; i--) { cout<<ways[j][i]; } return 0; } int ADD(int a[], int b[], int c[]) { int i, remainder, carry; for (i=1, carry=0; i<=b[0] && i<=c[0]; i++) { remainder = (b[i] + c[i] + carry) % 10; carry = (b[i] + c[i] + carry) / 10; a[i] = remainder; } for (; i<=b[0]; i++) { remainder = (b[i] + carry) % 10; carry = (b[i] + carry) / 10; a[i] = remainder; } for (; i<=c[0]; i++) { remainder = (c[i] + carry) % 10; carry = (c[i] + carry) / 10; a[i] = remainder; } if (carry==1) { a[i++] = carry; } a[0] = i - 1; return 0; } int MUL(int a[], int k) { int i, remainder, carry=0; for (i=1; i<=a[0]; i++) { remainder = (a[i] * k + carry) % 10; carry = (a[i] * k + carry) / 10; a[i] = remainder; } for (; carry>0; i++) { remainder = carry % 10; carry = carry / 10; a[i] = remainder; } a[0] = i - 1; return 0; } | I can't find any differences between 1009 and 1012! | Fu Jieyun | 1012. K-based Numbers. Version 2 | 4 Apr 2004 11:57 | 4 | Please tell me if there are any differences between 1009 and 1012. I got AC in 1009 but got WA in 1012 Quote: "You may assume that 2 <= K <= 10; 2 <= N; 4 <= N+K <= 180." This means you have to use BIG numbers! the number of 1012 is bigger | PLEASE, GIVE ME SOME TESTS !!! | vano_B1 | 1012. K-based Numbers. Version 2 | 16 Jul 2003 23:17 | 2 | | This can't be wrong,why??? | zeratul | 1012. K-based Numbers. Version 2 | 6 Jul 2003 12:16 | 3 | program xc1; var i:integer; a0,a1,a2:longint; n,k:integer; begin read(n,k); a0:=1; a1:=k-1; for i:=2 to n do begin a2:=(k-1)*(a0+a1); a0:=a1; a1:=a2; end; writeln(a2); end. Your logic seems OK (I didn't check it, but it seems). Your problem is that the numbers might (and will) be bigger than a longint (not in the input but in the output). number 1009 you must use int64 number 1012 you must use your own count arithmatic | Here is my program but why it is wrong? | carol_m1 | 1012. K-based Numbers. Version 2 | 18 Jun 2003 20:09 | 1 | program knumber; var k,n,i,t:integer; a,b,c:array[1..2000] of integer; procedure multiply; var i,g:longint; begin g:=0; for i:=1 to t do begin b[i]:=b[i]+a[i]+g; g:=b[i] div 10; b[i]:=b[i] mod 10; end; if g<>0 then begin inc(t); b[t]:=g; end; g:=0; for I:=1 to t do begin b[i]:=b[i]*(k-1)+g; g:=b[i] div 10; b[i]:=b[i] mod 10; end; while g<>0 do begin inc(t); b[t]:=g mod 10; g:=g div 10; end; end; begin readln(n); readln(k); a[1]:=1; t:=0;i:=k-1; while i>0 do begin inc(t); b[t]:=i mod 10; i:=i div 10; end; for i:=2 to n do begin c:=b; multiply; a:=c; end; for i:=t downto 1 do write(b[i]); writeln; end. | 1012 My Code Help | daiwb | 1012. K-based Numbers. Version 2 | 18 Jun 2003 09:20 | 1 | #include <iostream> #include <iomanip> #include <cmath> using namespace std; long c(double n,double m){ long i; double result=1; for(i=(long)m+1;i<=(long)n;i++){ result*=(double)i/(double)(i-m); } return (long)result; } int main(void){ long num_zero,i; double k,n,sum=0; cin>>n>>k; num_zero=(long)n/2; for(i=0;i<=num_zero;i++){ sum+=pow(k-1,n-i)*c(n-i,i); } cout<<setiosflags(ios::fixed)<<setprecision(0)<<sum<<endl; return 0; } | Problems 1012, 1013 | Leo | 1012. K-based Numbers. Version 2 | 2 Dec 2002 21:41 | 2 | I think, that there are some bugs in tests answers and judge solutions. My long ariphmetics with bugs gets AC and without - WA. All problems were in multiplication. I had overflow of WORD and got AC. Who knows, how to post my opinion to site Creators. you can use an array of integers for that huge number . when n = 1800 and k = 10 the answer have about 3600 digits !!! and you cant store it in nor long nor any other variable. in this problem you must find a recursive relations . ( not to consider all answer ). it has a nice soloution . if you use this method but not receive the correct answer mail me at : ioi_khafan@hotmail.com . thanks . bye | Why my program give WA ? | Accepted | 1012. K-based Numbers. Version 2 | 2 Dec 2002 14:53 | 1 | this is my C prog . i don know why this prog give WA ? anyone how know it please mail me at : SSF_DIGI@HOTMAIL.COM thanks SABI #include <stdio.h> int n,k,z,lb,a[360],b[360]; void jam() { int temp[360]; int i; for (i=0;i<=lb;i++) temp[i] = b[i]; for (i=0;i<=lb;i++) { b[i] = a[i] + b[i]; if (b[i] > 9) { b[i] = b[i] - 10; b[i+1] = b[i+1] + 1; } } for (i=0;i<=lb;i++) a[i] = temp[i]; if (b[lb+1] > 0) lb++; } void zarb() { int j; for (j = 0 ;j <= lb ; j ++) { b[j] = b[j] * (k-1); if (b[j] > 9) { b[j] = b[j]/10; b[j+1] = b[j+1] + b[j]%10; } } if (b[lb+1] > 0) lb++; } void main(void) { scanf ("%d\n%d",&n,&k); a[0] = 0; b[0] = k; lb = 0; if (b[0] = 10) { b[0] = 0; b[1] = 1; lb = 1; } for (z=1;z<n-1;z++) { jam(); zarb(); } for (z=0;z<=lb;z++) { b[z] = b[z] * (k-1); if (b[z] > 9) { b[z+1] = b[z]/10; b[z] = b[z]%10; } } if (b[lb+1] > 0) lb++; for (z=lb;z>=0;z--) printf ("%d",b[z]); } | Why do I get Wrong Answer ? Here is my program: | Costel::icerapper@k.ro | 1012. K-based Numbers. Version 2 | 20 Nov 2002 13:10 | 2 | PROGRAM SAVEMAN_TIMUS; CONST MAXDIGIT = 180; NODEDIGIT = 8; MAXVALUE = 99999999; MODULATOR = 100000000; TYPE BigNumber=array[1..MAXDIGIT div NODEDIGIT]of longint; PROCEDURE INIT(var b:BigNumber); begin fillchar(b,sizeof(b),0);end; PROCEDURE ADDINT(var b:BigNumber;int:longint); var i:longint; begin for i:=1 to MAXDIGIT div NODEDIGIT do begin b[i]:=b[i]+int; int:=b[i] div MODULATOR; b[i]:=b[i] mod MODULATOR; if int=0 then exit; end; end; PROCEDURE ADDBIG(var b:BigNumber;n:BigNumber); var i:longint; REST:longint;begin REST:=0; for i:=1 to MAXDIGIT div NODEDIGIT do begin b[i]:=b[i]+n[i]+REST; REST:=b[i] div MODULATOR; b[i]:=b[i] mod MODULATOR; end;end; PROCEDURE MULINT(var b:BigNumber;int:longint); var i:longint;REST:longint;begin for i:=1 to MAXDIGIT div NODEDIGIT do begin b[i]:=b[i]*int+REST; REST:=b[i] div MODULATOR;b[i]:=b[i] mod MODULATOR; end;end; PROCEDURE PRINTBIG(b:BigNumber); var k,i,l:longint; s:string;begin for i:=MAXDIGIT div NODEDIGIT downto 1 do if b[i]<>0 then break; write(b[i]); k:=i-1; for i:=k downto 1 do begin Str(b[i]:8,s); l:=1; while (s[l]=' ')and(l<=8) do begin s[l]:='0';inc(l);end; write(s); end; end; var A0,B0,A1:BigNumber; n,k:longint; i:longint; begin readln(n,k); init(a0); init(b0); ADDINT(b0,k-1); for i:=2 to n do begin A1:=B0; {a(i)=b(i-1)} ADDBIG(b0,a0); {b(i)=b(i-1)+a(i-1)} MULINT(b0,k-1);{b(i)=b(i-1)*(k-1)} A0:=A1; {inc(i)} end; ADDBIG(A1,B0); {a(n)=a(n)+b(n)} PRINTBIG(A1); end. b[i]*int may be more than MaxLongInt | Output format | Nazar Revutsky | 1012. K-based Numbers. Version 2 | 28 Sep 2001 05:43 | 2 | What is the output format for this problem??? I try with long int but get WA. With double I also have WA. I made 1009 program and trry make 1012 with same formula. Please HELP. |
|
|