Google Code Jam.
This commit is contained in:
157
google_code_jam/2014/1B-B-lottery.cpp
Normal file
157
google_code_jam/2014/1B-B-lottery.cpp
Normal file
@@ -0,0 +1,157 @@
|
||||
/* Check cf5-opt.vim defs.
|
||||
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
|
||||
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
|
||||
VIM: let g:cppflags=g:Iboost.g:Itbb
|
||||
VIM: let g:ldflags=g:Lboost.g:Ltbb.g:tbbmalloc.g:tbbmproxy
|
||||
VIM: let g:ldlibpath=g:Bboost.g:Btbb
|
||||
VIM: let g:argv=""
|
||||
*/
|
||||
|
||||
/*
|
||||
New Lottery Game
|
||||
|
||||
The Lottery is changing! The Lottery used to have a machine to generate a random winning number. But due to cheating problems, the Lottery has decided to add another machine. The new winning number will be the result of the bitwise-AND operation between the two random numbers generated by the two machines.
|
||||
|
||||
To find the bitwise-AND of X and Y, write them both in binary; then a bit in the result in binary has a 1 if the corresponding bits of X and Y were both 1, and a 0 otherwise. In most programming languages, the bitwise-AND of X and Y is written X&Y.
|
||||
|
||||
For example:
|
||||
The old machine generates the number 7 = 0111.
|
||||
The new machine generates the number 11 = 1011.
|
||||
The winning number will be (7 AND 11) = (0111 AND 1011) = 0011 = 3.
|
||||
|
||||
With this measure, the Lottery expects to reduce the cases of fraudulent claims, but unfortunately an employee from the Lottery company has leaked the following information: the old machine will always generate a non-negative integer less than A and the new one will always generate a non-negative integer less than B.
|
||||
|
||||
Catalina wants to win this lottery and to give it a try she decided to buy all non-negative integers less than K.
|
||||
|
||||
Given A, B and K, Catalina would like to know in how many different ways the machines can generate a pair of numbers that will make her a winner.
|
||||
|
||||
Could you help her?
|
||||
|
||||
Input
|
||||
|
||||
The first line of the input gives the number of test cases, T. T lines follow, each line with three numbers A B K.
|
||||
|
||||
Output
|
||||
|
||||
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the number of possible pairs that the machines can generate to make Catalina a winner.
|
||||
|
||||
Limits
|
||||
|
||||
1 ? T ? 100.
|
||||
Small dataset
|
||||
|
||||
1 ? A ? 1000.
|
||||
1 ? B ? 1000.
|
||||
1 ? K ? 1000.
|
||||
Large dataset
|
||||
|
||||
1 ? A ? 109.
|
||||
1 ? B ? 109.
|
||||
1 ? K ? 109.
|
||||
Sample
|
||||
|
||||
|
||||
Input
|
||||
|
||||
Output
|
||||
|
||||
5
|
||||
3 4 2
|
||||
4 5 2
|
||||
7 8 5
|
||||
45 56 35
|
||||
103 143 88
|
||||
|
||||
Case #1: 10
|
||||
Case #2: 16
|
||||
Case #3: 52
|
||||
Case #4: 2411
|
||||
Case #5: 14377
|
||||
In the first test case, these are the 10 possible pairs generated by the old and new machine respectively that will make her a winner: <0,0>, <0,1>, <0,2>, <0,3>, <1,0>, <1,1>, <1,2>, <1,3>, <2,0> and <2,1>. Notice that <0,1> is not the same as <1,0>. Also, although the pair <2, 2> could be generated by the machines it wouldn't make Catalina win since (2 AND 2) = 2 and she only bought the numbers 0 and 1.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <limits>
|
||||
#include <math.h>
|
||||
|
||||
typedef long long i64;
|
||||
typedef std::vector<i64> vec;
|
||||
void check( bool b ) { if ( !b )
|
||||
std::cerr << "error" << std::endl; }
|
||||
#define FOR(i,l) for ( i64 i =0, ie = i64(l); i<ie; ++i )
|
||||
/*
|
||||
Read n values into v
|
||||
*/
|
||||
template <class V>
|
||||
void readv( V& v, int n )
|
||||
{
|
||||
v.reserve(n);
|
||||
for ( int i = 0; i < n; ++i )
|
||||
{
|
||||
typename V::value_type e;
|
||||
std::cin >> e;
|
||||
check( !std::cin.fail() );
|
||||
v.push_back(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int solve_puzzle()
|
||||
{
|
||||
int a, b, k;
|
||||
std::cin >> a >> b >> k;
|
||||
|
||||
i64 s = 0;
|
||||
for ( int i = 0; i < k; ++i )
|
||||
for ( int i = 0; i < a; i++ )
|
||||
for ( int j = 0; j < b; j++ )
|
||||
if ( (i&j) < k )
|
||||
s++;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
#undef int
|
||||
int main ( void )
|
||||
{try{
|
||||
srand((unsigned)time(NULL));
|
||||
int puzzle_count;
|
||||
|
||||
std::cin >> puzzle_count;
|
||||
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
|
||||
for ( int i = 1; i <= puzzle_count; i++ )
|
||||
{
|
||||
std::cout << "Case #" << i << ": ";
|
||||
auto r = solve_puzzle();
|
||||
std::cout << r << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
catch ( const std::exception& e )
|
||||
{
|
||||
std::cerr << std::endl
|
||||
<< "std::exception(\"" << e.what() << "\")." << std::endl;
|
||||
return 2;
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
std::cerr << std::endl
|
||||
<< "unknown exception." << std::endl;
|
||||
return 1;
|
||||
}}
|
||||
Reference in New Issue
Block a user