← Back to topics
Topic

proizvod

k
koftriot12
I was trying to solve proizvod task but the most i could solve were only two test cases.

I mean its ok for factorial till 12 but after that is the problem.

For greater numbers then 12 i tried multiplying the last digits it look something like this:
while(n>0)
fac*=n;
result*=last_digit(fac);
n--;
}
printf("%ld",last_digit(result));

But it failed any ideas.
A
Al3kSaNdaR
The idea is that in every step of calculation you remind the number with 10 while his last number is 0 and then mod it with 100000 just to be sure that you don't have WA , and then print number % 10.
g
gigac
What's wrong with my code ?
D
Diabolic
Al3kSaNdaR could you please explain which is that number with 10 ??
A
Al3kSaNdaR
Final number, you use for ( i = 1; i <= n; i++ )
and multiply some number T with i. You must div T by 10 as long as T % 10 == 0. And inside for loop at her end you must put T = T % 100000. 100000 is 10 times larger than maximum number in input so you mod it by 100000. And finally you output T % 10.
D
Diabolic
Yes, that's what I did, except for T=T0000. Probably this reduces the number to much lower one. Thanks for the tip.
A
Al3kSaNdaR
Np, you must use that because of the overflow. ;)
d
delix
Ok!, my solution is that of multiplying only the last digit with a numbers starting from n down to 1 and it works fine for all the test cases i have tried, but for numbers greater that 30! i don't know which is the last digit, so i can not check if my solution is correct, where can i find the last digits of >30! numbers like 46!or 100!?
A
Asmirasmir
you dont need to test it for so big test cases , just look not to have overflow or that you go over the time limit.

Now heres a algo to solve it.
You start with 1 now multiply it with 2 ...
Now that works until you get overflow (when you get over the limit that an variable can store)
So the solution is to look at the last digit (but you get incorrect results ) look at this:
350*4=1400 so this is the correct one
and 5*4=20 and this not.

Now the solution is:
read n
for i=1 to n
p = p*i;
while p mod 10 = 0
p=p/10;
p=p mod 1000 000 (note that in this task you can use mod 100 000 but in some tasks you need more - exactly max num + 1 zero)

after that just print the answer