Advertisement
golergka

spoj.pl Factorial final

May 29th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.85 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Factorial
  4. {
  5.     class MainClass
  6.     {
  7.        
  8.         static int fivePowerSum (int x)
  9.         {
  10.             return (int)((Math.Pow (5, x) - 1) / 4);
  11.         }
  12.        
  13.         static int z (int x)
  14.         {
  15.             if (x < 5)
  16.                 return 0;
  17.            
  18.             int result = 0;
  19.             int log;
  20.            
  21.             do {
  22.                
  23.                 log = -1;
  24.                 int fivePower = 1;
  25.                 do {
  26.                     fivePower *= 5;
  27.                     log++;
  28.                 } while (x>=fivePower);
  29.                
  30.                 int fivePowerDiv5 = fivePower / 5;
  31.                 result += (fivePowerDiv5 - 1) / 4;
  32.                 x -= fivePowerDiv5;
  33.                
  34.             } while (log>0);
  35.            
  36.             return result;
  37.            
  38.         }
  39.        
  40.         public static void Main (string[] args)
  41.         {
  42.             int testCases = Convert.ToInt32 (Console.ReadLine ());
  43.             int[] xs = new int[testCases];
  44.             for (int i=0; i<testCases; i++)
  45.                 xs [i] = Convert.ToInt32 (Console.ReadLine ());
  46.             foreach (int x in xs)
  47.                 Console.WriteLine (z (x));
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement