Sunday, November 3, 2013

codechef FCTRL - Factorial c++ solution

codechef FCTRL - Factorial problem: http://www.codechef.com/problems/FCTRL

Factorial

All submissions for this problem are available.


The most important part of a GSM network is so called
Base Transceiver Station (BTS). These transceivers form the
areas called cells (this term gave the name to the cellular phone)
and every phone connects to the BTS with the strongest signal (in
a little simplified view). Of course, BTSes need some attention and
technicians need to check their function periodically.


The technicians faced a very interesting problem recently. Given a set of
BTSes to visit, they needed to find the shortest path to visit all of the
given points and return back to the central company building. Programmers
have spent several months studying this problem but with no results. They
were unable to find the solution fast enough. After a long time, one of the
programmers found this problem in a conference article. Unfortunately, he
found that the problem is so called "Traveling Salesman Problem" and it is
very hard to solve. If we have N BTSes to be visited, we can visit them in
any order, giving us N! possibilities to examine. The function expressing
that number is called factorial and can be computed as a product
1.2.3.4....N. The number is very high even for a relatively small N.


The programmers understood they had no chance to solve the problem. But
because they have already received the research grant from the government,
they needed to continue with their studies and produce at least some
results. So they started to study behavior of the factorial function.


For example, they defined the function Z. For any positive integer N,
Z(N) is the number of zeros at the end of the decimal form of number
N!. They noticed that this function never decreases. If we have two numbers
N1<N2, then
Z(N1) <= Z(N2). It is because we can never "lose" any
trailing zero by multiplying by any positive number. We can only get new
and new zeros. The function Z is very interesting, so we need a computer
program that can determine its value efficiently.

Input


There is a single positive integer T on the first line of input (equal to about 100000). It stands
for the number of numbers to follow. Then there are T lines, each containing
exactly one positive integer number N,
1 <= N <= 1000000000.

Output


For every number N, output a single line containing the single non-negative
integer Z(N).

Example

Sample Input:
6
3
60
100
1024
23456
8735373
Sample Output:
0
14
24
253
5861
2183837



and here is my c++ solution to codechef FCTRL - Factorial : http://ideone.com/QmVocm

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;

int main() {
 // your code goes here
 int a, b, c;
 cin>>a;
 while(a--) {
  cin>>b;
  c=0;
  for (int i=1; pow(5, i)<=b; i++) {
   c=c+b/pow(5, i);
   //i++;
  }
  cout<<c<<endl;
 }
 return 0;
}

codechef INTEST - Enormous Input Test c++ solution

codechef INTEST - Enormous Input Test problem: http://www.codechef.com/problems/INTEST

All submissions for this problem are available.

The purpose of this problem is to verify whether the method you are using to read input data is sufficiently fast to handle problems branded with the enormous Input/Output warning. You are expected to be able to process at least 2.5MB of input data per second at runtime.

Input

The input begins with two positive integers n k (n, k<=107). The next n lines of input contain one positive integer ti, not greater than 109, each.

Output

Write a single integer to output, denoting how many integers ti are divisible by k.

Example

Input:
7 3
1
51
966369
7
9
999996
11

Output:
4


and here is my codechef INTEST - Enormous Input Test c++ solution: http://ideone.com/T05cEX

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
 // your code goes here
 int a, b, c, d=0;
 scanf("%d %d", &a, &b);
 while (a--) {
  scanf("%d", &c);
  if(c%b==0) d++;
 }
 printf("%d", d);
 return 0;
}

Note: I was looking for "buffer c++" all over the web, but I could not get over it. c++ "cin" and "cout" makes some WA. so, instead, just try c "scanf" and "printf", that would be helpful, and easy to understand. "buffer" is extremely difficult to me to understand the code. good luck guys.

Saturday, November 2, 2013

buffer c++, Unix and gnu gcc buffering

Here buffering is explained: http://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html

Stream Buffers

Derived streambuf Classes


Creating your own stream buffers for I/O can be remarkably easy. If you are interested in doing so, we highly recommend two very excellent books: Standard C++ IOStreams and Locales by Langer and Kreft, ISBN 0-201-18395-1, and The C++ Standard Library by Nicolai Josuttis, ISBN 0-201-37926-0. Both are published by Addison-Wesley, who isn't paying us a cent for saying that, honest.
Here is a simple example, io/outbuf1, from the Josuttis text. It transforms everything sent through it to uppercase. This version assumes many things about the nature of the character type being used (for more information, read the books or the newsgroups):
    #include <iostream>
    #include <streambuf>
    #include <locale>
    #include <cstdio>

    class outbuf : public std::streambuf
    {
      protected:
 /* central output function
  * - print characters in uppercase mode
  */
 virtual int_type overflow (int_type c) {
     if (c != EOF) {
  // convert lowercase to uppercase
  c = std::toupper(static_cast<char>(c),getloc());

  // and write the character to the standard output
  if (putchar(c) == EOF) {
      return EOF;
  }
     }
     return c;
 }
    };

    int main()
    {
 // create special output buffer
 outbuf ob;
 // initialize output stream with that output buffer
 std::ostream out(&ob);

 out << "31 hexadecimal: "
     << std::hex << 31 << std::endl;
 return 0;
    }
   
Try it yourself! More examples can be found in 3.1.x code, in include/ext/*_filebuf.h, and in this article by James Kanze: Filtering Streambufs.

Buffering

First, are you sure that you understand buffering? Particularly the fact that C++ may not, in fact, have anything to do with it?
The rules for buffering can be a little odd, but they aren't any different from those of C. (Maybe that's why they can be a bit odd.) Many people think that writing a newline to an output stream automatically flushes the output buffer. This is true only when the output stream is, in fact, a terminal and not a file or some other device -- and that may not even be true since C++ says nothing about files nor terminals. All of that is system-dependent. (The "newline-buffer-flushing only occurring on terminals" thing is mostly true on Unix systems, though.)
Some people also believe that sending endl down an output stream only writes a newline. This is incorrect; after a newline is written, the buffer is also flushed. Perhaps this is the effect you want when writing to a screen -- get the text out as soon as possible, etc -- but the buffering is largely wasted when doing this to a file:
   output << "a line of text" << endl;
   output << some_data_variable << endl;
   output << "another line of text" << endl; 
The proper thing to do in this case to just write the data out and let the libraries and the system worry about the buffering. If you need a newline, just write a newline:
   output << "a line of text\n"
   << some_data_variable << '\n'
   << "another line of text\n"; 
I have also joined the output statements into a single statement. You could make the code prettier by moving the single newline to the start of the quoted text on the last line, for example.
If you do need to flush the buffer above, you can send an endl if you also need a newline, or just flush the buffer yourself:
   output << ...... << flush;    // can use std::flush manipulator
   output.flush();               // or call a member fn 
On the other hand, there are times when writing to a file should be like writing to standard error; no buffering should be done because the data needs to appear quickly (a prime example is a log file for security-related information). The way to do this is just to turn off the buffering before any I/O operations at all have been done (note that opening counts as an I/O operation):
   std::ofstream    os;
   std::ifstream    is;
   int   i;

   os.rdbuf()->pubsetbuf(0,0);
   is.rdbuf()->pubsetbuf(0,0);

   os.open("/foo/bar/baz");
   is.open("/qux/quux/quuux");
   ...
   os << "this data is written immediately\n";
   is >> i;   // and this will probably cause a disk read 
Since all aspects of buffering are handled by a streambuf-derived member, it is necessary to get at that member with rdbuf(). Then the public version of setbuf can be called. The arguments are the same as those for the Standard C I/O Library function (a buffer area followed by its size).
A great deal of this is implementation-dependent. For example, streambuf does not specify any actions for its own setbuf()-ish functions; the classes derived from streambuf each define behavior that "makes sense" for that class: an argument of (0,0) turns off buffering for filebuf but does nothing at all for its siblings stringbuf and strstreambuf, and specifying anything other than (0,0) has varying effects. User-defined classes derived from streambuf can do whatever they want. (For filebuf and arguments for (p,s) other than zeros, libstdc++ does what you'd expect: the first s bytes of p are used as a buffer, which you must allocate and deallocate.)
A last reminder: there are usually more buffers involved than just those at the language/library level. Kernel buffers, disk buffers, and the like will also have an effect. Inspecting and changing those are system-dependent.