Showing posts with label biginterger. Show all posts
Showing posts with label biginterger. Show all posts

Thursday, 1 January 2015

Small factorial (FCTRL2) spoj

small factorial use of Big integer in java

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
import java.util.Scanner;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes heres
int n,m,k,l,i;
BigInteger a,x,c;
a=new BigInteger("1");
Scanner w=new Scanner(System.in);
int t= w.nextInt();
while(t>0)
{
t--;
m=1;
a=new BigInteger("1");
n=w.nextInt();
for(i=1;i<=n;i++)
{ x=new BigInteger(Integer.toString(i));
a=a.multiply(x);


}
System.out.println(a);

}
/*BigInteger a=new BigInteger(Integer.toString(t));
System.out.println(a);*/
}
}

Monday, 15 December 2014

GCD 2 (spoj) , num b is type string

GCD2

in spoj though seems to e bit of tedious and boring question , its really small and has some good coding concept behind it which can be used when we encounter a situation where need a a big string to be handled and at the same time we need to find the gcd of it.

solu
we can take the bigger num input as a string and while converting it number be do %a all the time , and apply the normal procedure. :P

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. int gcd(int a,int b){
  5. if(b==0)
  6. return a;
  7. else
  8. gcd(b,a%b);
  9. }
  10. int main(){
  11. int t;
  12. scanf("%d",&t);
  13. while(t--){
  14. int a,i,len,b=0;
  15. char n[250];
  16. scanf("%d%s",&a,n);
  17. len=strlen(n);
  18. if(a==0){
  19. printf("%s\n",n);
  20. continue;
  21. }
  22. for(i=0;i<len;i++)
  23. b=(b*10+(n[i]-'0'))%a;
  24. printf("%d\n",gcd(a,b));
  25. }
  26. return 0;
  27. }