- 1

what is the problem?

Sample Input 3 2 2 4 8 1 3 2 3 Sample Output 1 3 Explanation For query 1: GCD(F(2) , F(4), F(8))= GCD(1,3,21)=1 For query 2: GCD(F(4), F(8))= GCD(3,21)=3 import java.util.*; class TestClass { public static void main(String args[] ) throws Exception { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int q=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); for(int i=0;i<q;i++) { int l=sc.nextInt(); int r=sc.nextInt(); int gc=fibo(a[l-1]); // System.out.print(gc); for(int j=l;j<r;j++) { int y=fibo(a[j]); // System.out.print(y+" "); gc=gcd(gc,y); System.out.println(gc); } } static int gcd(int x,int e) { int p=1; while(x!=0){ int z=x; p=e/x; x=e%x; e=z; } return e; } static int fibo(int f) { int d=0,c=0; int a=1,b=0; while(d<f) { c=a+b; a=b; b=c; d++; } return c; }

11th Jun 2017, 6:36 PM
Somnath Ghosh
Somnath Ghosh - avatar
1 Odpowiedź
+ 2
Please tag the code and don't post the code. (You can edit it, and please do) Just makes it easier for everyone to test and experiment
11th Jun 2017, 6:50 PM
Limitless
Limitless - avatar