Check cp-algorithms.com it’s a good starting point
this is link to my solution CodeChef: Practical coding for everyone i used the logic that unit digit of fibonacci repeats after 60 digits and the selected element in array can be represented as a[2^n-1].
please tell me if code could be improved in some way
i also think the same, i think apart from the solution , its more challenging for a beginner to understand what mathematical concepts have been used , the editorial maker directly uses the various maths concept and make it harder , for beginners like me to understand .
so i think they should , go with more simpler approach to explain the logic.
another alternative is to use log2l instead of log2
It worked for me
Receiving RE for the third input. I have tried so many approaches but still issue persists.
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
int t;
int f[] = {0,1,1,2,3,5,8,3,1,4,5,9,4,3,7,0,7,7,4,1,5,6,1,7,8,5,3,8,1,9,0,9,9,8,7,5,2,7,9,6,5,1,6,7,3,0,3,3,6,9,5,4,9,3,2,5,7,2,9,1};
Scanner in = new Scanner(System.in);
t = in.nextInt();
while(t>0)
{
long n,i;
i = 1;
n = in.nextInt();
while(i<=n)
{
i = i*2;
}
i = i/2;
i=(i-1)%60;
System.out.println(f[(int)i]);
t--;
}
}```
Is there any way we can find where im getting RE?
Please format your code - the forum software has mangled it and it is no longer valid Java!
Edit:
Try the testcase
1
576460752303423487
Thanks, prblem is with the way Im reading the input. changed “nextInt()” to “nextLong()” and it worked fine.
This will help you alot : Easy Fibonacci | September long challenge 2019 | Codechef Solutions & Code || FIBEASY | Hello World - YouTube
see this thank you
where am i going wrong?
arr =[0,1]
for x in range(60-2):
arr.append((arr[-1]+arr[-2])%10)
def red(arr,n):
brr = arr[:n]
while(len(brr)!=1):
brr = brr[1::2]
return brr[0]
tc = int(input())
for i in range(tc):
num = int(input())
print(red(arr,num%60))
plss tell me where its exceeding time limit
Can someone point out the error in my code?
import java.util.*;
class Fibnocci {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int t,n,ans;
ArrayList<Integer>a = new ArrayList<Integer>();
ArrayList<Integer>e = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
t = sc.nextInt();
while(t>0){
n = sc.nextInt();
a.add(0);
a.add(1);
for(int i=2;i<n;i++){
a.add(a.get(i-1)+a.get(i-2));
}
for(int i=0;i<n;i++){
a.set(i, a.get(i)%10);
}
if(a.get(a.size()-1)==1){
while(a.size()>1){
for(int i=0;i<a.size();i++){
if(i%2==0){
a.set(i,-1);
}
}
for(int i=0;i<a.size();i++){
if(a.get(i)==-1){
a.remove(i);
}
}
}
}
ans=a.get(0);
System.out.println(ans);
t--;
}
}
}
For people who need more clear explanation of the editorial can go to the GitHub link: Easy Fibonacci
can anyone check why i am getting wrong answer on this::
double n = in.readDouble();
int x = (int)(Math.log(n) /
Math.log(2));
x = (int)Math.pow(2, x);
out.printLine(fib(x-1)%10);
out.flush();
//fib(x-1) is implemented in logn using matrix expo
@as_max_rider You can check your program output for the following test-cases:
4
9
72057594037927935
72057594037927936
18014398509481983
Output shoud be:
3
3
0
9
If your program is not giving the correct answer then read the editorial: Easy Fibonacci
Probably there is problem with how you are calculating log. Does your log function gives correct answers for large values.
NOTE: You don’t need to use matrix exponentiation.
You can also read the following post: FIBEASY Matrix Exponentiation Solution? O(lgn) (edit: this solution failed for subcase 3 Yet I am producing right answers?) - #3 by striker22
you were right!!
my problem was with the log function.
my idea was almost same as the editorial except i didn’t use the periodicity part…
i observed a relation among the numbers
hey everyone. cant i avoid TLE using this method i have done?
Hi,
I am seriously facing trouble understanding that. Can anybody give me an easy explanation.
Or handily you can avoid any sort of log function - use the bit_length() function of int class.