Skip to content

Commit f0d898e

Browse files
committed
SRM 616
1 parent cb77f10 commit f0d898e

File tree

4 files changed

+493
-0
lines changed

4 files changed

+493
-0
lines changed

TopCoder/SRM/616/ColorfulCoins.cpp

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
// BEGIN CUT HERE
2+
3+
// END CUT HERE
4+
#include <algorithm>
5+
#include <iostream>
6+
#include <sstream>
7+
#include <string>
8+
#include <vector>
9+
#include <queue>
10+
#include <set>
11+
#include <map>
12+
#include <cstdio>
13+
#include <cstdlib>
14+
#include <cctype>
15+
#include <cmath>
16+
#include <string>
17+
#include <cstring>
18+
using namespace std;
19+
20+
// BEGIN CUT HERE
21+
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
22+
23+
template<typename T> void print( T a ) {
24+
cerr << a;
25+
}
26+
static void print( long long a ) {
27+
cerr << a << "L";
28+
}
29+
static void print( string a ) {
30+
cerr << '"' << a << '"';
31+
}
32+
template<typename T> void print( vector<T> a ) {
33+
cerr << "{";
34+
for ( int i = 0 ; i != a.size() ; i++ ) {
35+
if ( i != 0 ) cerr << ", ";
36+
print( a[i] );
37+
}
38+
cerr << "}" << endl;
39+
}
40+
template<typename T> void eq( int n, T have, T need ) {
41+
if ( have == need ) {
42+
cerr << "Case " << n << " passed." << endl;
43+
} else {
44+
cerr << "Case " << n << " failed: expected ";
45+
print( need );
46+
cerr << " received ";
47+
print( have );
48+
cerr << "." << endl;
49+
}
50+
}
51+
template<typename T> void eq( int n, vector<T> have, vector<T> need ) {
52+
if( have.size() != need.size() ) {
53+
cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements.";
54+
print( have );
55+
print( need );
56+
return;
57+
}
58+
for( int i= 0; i < have.size(); i++ ) {
59+
if( have[i] != need[i] ) {
60+
cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << ".";
61+
print( have );
62+
print( need );
63+
return;
64+
}
65+
}
66+
cerr << "Case " << n << " passed." << endl;
67+
}
68+
static void eq( int n, string have, string need ) {
69+
if ( have == need ) {
70+
cerr << "Case " << n << " passed." << endl;
71+
} else {
72+
cerr << "Case " << n << " failed: expected ";
73+
print( need );
74+
cerr << " received ";
75+
print( have );
76+
cerr << "." << endl;
77+
}
78+
}
79+
// END CUT HERE
80+
81+
#define REP(i,n) for(int i=0;i<(n);++i)
82+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
83+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
84+
#define FOREACH(it,c) for(typeof((c).begin())it=(c).begin();it!=(c).end();++it)
85+
#define CLR(x) memset((x),0,sizeof((x)))
86+
#define MP make_pair
87+
#define MPI make_pair<int, int>
88+
#define PB push_back
89+
typedef long long LL;
90+
typedef vector<int> VI;
91+
typedef vector<string> VS;
92+
typedef pair<int, int> PI;
93+
94+
// BEGIN CUT HERE
95+
vector<string> split( const string& s, const string& delim =" " ) {
96+
vector<string> res;
97+
string t;
98+
for ( int i = 0 ; i != s.size() ; i++ ) {
99+
if ( delim.find( s[i] ) != string::npos ) {
100+
if ( !t.empty() ) {
101+
res.push_back( t );
102+
t = "";
103+
}
104+
} else {
105+
t += s[i];
106+
}
107+
}
108+
if ( !t.empty() ) {
109+
res.push_back(t);
110+
}
111+
return res;
112+
}
113+
114+
vector<int> splitInt( const string& s, const string& delim =" " ) {
115+
vector<string> tok = split( s, delim );
116+
vector<int> res;
117+
for ( int i = 0 ; i != tok.size(); i++ )
118+
res.push_back( atoi( tok[i].c_str() ) );
119+
return res;
120+
}
121+
// END CUT HERE
122+
123+
// BEGIN CUT HERE
124+
int s2i(string s) {
125+
stringstream ss;
126+
ss << s;
127+
int res;
128+
ss >> res;
129+
return res;
130+
}
131+
132+
string i2s(int n) {
133+
stringstream ss;
134+
ss << n;
135+
string res;
136+
ss >> res;
137+
return res;
138+
}
139+
// END CUT HERE
140+
141+
class ColorfulCoins {
142+
public:
143+
bool isok(LL a, LL b, LL c) {
144+
return b * log(a) > log(c);
145+
}
146+
int minQueries(vector<long long> values) {
147+
int n = values.size();
148+
if (n == 1) return 1;
149+
vector<LL> mm;
150+
FOR(i,1,n-1) mm.PB(values[i] / values[i - 1]);
151+
sort(mm.begin(), mm.end());
152+
int res = 0;
153+
int m = n - 1;
154+
while (++res) {
155+
bool ok = true;
156+
REP(i,m) {
157+
if (!isok(mm[i], res, i + 1)) {
158+
ok = false;
159+
break;
160+
}
161+
}
162+
if (ok) break;
163+
}
164+
return res;
165+
}
166+
167+
};
168+
// BEGIN CUT HERE
169+
int main() {
170+
{
171+
long long valuesARRAY[] = {1LL};
172+
vector<long long> values( valuesARRAY, valuesARRAY+ARRSIZE(valuesARRAY) );
173+
ColorfulCoins theObject;
174+
eq(0, theObject.minQueries(values),1);
175+
}
176+
{
177+
long long valuesARRAY[] = {1LL, 3LL};
178+
vector<long long> values( valuesARRAY, valuesARRAY+ARRSIZE(valuesARRAY) );
179+
ColorfulCoins theObject;
180+
eq(1, theObject.minQueries(values),1);
181+
}
182+
{
183+
long long valuesARRAY[] = {1LL, 2LL, 4LL};
184+
vector<long long> values( valuesARRAY, valuesARRAY+ARRSIZE(valuesARRAY) );
185+
ColorfulCoins theObject;
186+
eq(2, theObject.minQueries(values),2);
187+
}
188+
{
189+
long long valuesARRAY[] = {1LL, 2LL, 4LL, 8LL, 16LL};
190+
vector<long long> values( valuesARRAY, valuesARRAY+ARRSIZE(valuesARRAY) );
191+
ColorfulCoins theObject;
192+
eq(3, theObject.minQueries(values),3);
193+
}
194+
{
195+
long long valuesARRAY[] = {1LL, 2LL, 6LL, 30LL, 90LL, 270LL, 810LL, 2430LL, 7290LL, 29160LL, 87480LL, 262440LL, 787320LL, 3149280LL,
196+
9447840LL, 28343520LL, 56687040LL, 170061120LL, 510183360LL, 1530550080LL, 3061100160LL,
197+
9183300480LL, 27549901440LL, 82649704320LL, 247949112960LL, 1239745564800LL, 3719236694400LL,
198+
14876946777600LL, 44630840332800LL, 223154201664000LL, 669462604992000LL, 2008387814976000LL,
199+
6025163444928000LL, 12050326889856000LL, 24100653779712000LL, 72301961339136000LL,
200+
289207845356544000LL, 867623536069632000LL};
201+
vector<long long> values( valuesARRAY, valuesARRAY+ARRSIZE(valuesARRAY) );
202+
ColorfulCoins theObject;
203+
eq(4, theObject.minQueries(values),4);
204+
}
205+
return 0;
206+
}
207+
// END CUT HERE
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html><body bgcolor="#ffffff" text="#000000"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td>The currency system in Colorland consists of various types of coins. The coin denominations follow these simple rules:
2+
3+
<ul>
4+
<li>The denominations are distinct positive integers.</li>
5+
<li>There is a coin type with denomination 1.</li>
6+
<li>For each pair of different coin types, the denomination of one coin type divides the denomination of the other one.</li>
7+
</ul>
8+
9+
You are given a long[] <b>values</b> containing all the available denominations in ascending order.
10+
<br></br><br></br>
11+
Coins of different denominations look exactly the same except that they have different colors. Each coin in Colorland has exactly one color. The coin colors follow these even simpler rules:
12+
13+
<ul>
14+
<li>All coins of the same type are of the same color.</li>
15+
<li>No two coins of different types are of the same color.</li>
16+
</ul>
17+
18+
You know all coin denominations used in Colorland, but you don't know their colors. You don't even know the set of colors used on the coins.
19+
<br></br><br></br>
20+
For each denomination, you'd like to know the color of coins of this denomination. To accomplish this, you've got a credit card with an infinite amount of money. You can perform queries to an ATM which can also provide you with an infinite amount of money. Each query is described by a positive integer X, which means that you want to receive exactly X units of money from the ATM. The ATM will provide you with the requested amount. You also know that the requested amount will be paid using the smallest possible number of coins. (Note that this rule always uniquely determines the set of coins chosen to make the payment.)
21+
<br></br><br></br>
22+
Return the smallest number of queries you need to determine the corresponding color for each of the denominations. (Note that this can always be done in a finite number of queries.)
23+
</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>ColorfulCoins</td></tr><tr><td>Method:</td><td>minQueries</td></tr><tr><td>Parameters:</td><td>vector&lt;long long&gt;</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int minQueries(vector&lt;long long&gt; values)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>256</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>values</b> will contain between 1 and 60 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>values</b> will be between 1 and 10^18, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>values</b> will be sorted in strictly ascending order. Note that this also implies that all the elements of <b>values</b> will be distinct.</td></tr><tr><td align="center" valign="top">-</td><td>For each pair of different elements in <b>values</b>, the smaller one will be a divisor of the larger one.</td></tr><tr><td align="center" valign="top">-</td><td><b>values</b>[0] will be 1.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2">There is just one coin type. We have to make a query to learn the color of coins.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1, 3}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2">Just one query with X = 5 is one possible solution. As the ATM gives the smallest number of coins, it will give one coin of denomination 3 and two coins of denomination 1. That means, for example, that if you get one red coin and two blue coins, you'll understand that coins of denomination 3 are red, and coins of denomination 1 are blue.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1, 2, 4}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">One optimal solution is to make two queries, first X = 5 and then X = 6. After the first query you'll receive one coin from each of denominations 1 and 4, and after the second query you'll receive one coin from each of denominations 2 and 4. Now you can uniquely determine the color of each denomination. For example, coins of denomination 4 have the color which appears twice among the received coins.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1, 2, 4, 8, 16}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1, 2, 6, 30, 90, 270, 810, 2430, 7290, 29160, 87480, 262440, 787320, 3149280,
24+
9447840, 28343520, 56687040, 170061120, 510183360, 1530550080, 3061100160,
25+
9183300480, 27549901440, 82649704320, 247949112960, 1239745564800, 3719236694400,
26+
14876946777600, 44630840332800, 223154201664000, 669462604992000, 2008387814976000,
27+
6025163444928000, 12050326889856000, 24100653779712000, 72301961339136000,
28+
289207845356544000, 867623536069632000}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 4</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

0 commit comments

Comments
 (0)