Skip to content

Commit e6e3c56

Browse files
committed
TCO14 R2B
1 parent c710927 commit e6e3c56

File tree

4 files changed

+555
-0
lines changed

4 files changed

+555
-0
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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+
#define MAXN 5000005
141+
142+
int plist[1000000], pcount;
143+
bool isP[MAXN], valid[MAXN];
144+
145+
int prime(int n){
146+
int i;
147+
if ((n!=2&&!(n%2))||(n!=3&&!(n%3))||(n!=5&&!(n%5))||(n!=7&&!(n%7)))
148+
return 0;
149+
for (i=0;plist[i]*plist[i]<=n;i++) {
150+
if (!(n%plist[i])) return 0;
151+
}
152+
return n>1;
153+
}
154+
155+
void initprime(){
156+
pcount = 0;
157+
memset(isP, false, sizeof(isP));
158+
isP[2] = 1;
159+
int i;
160+
for (plist[pcount++] = 2, i = 3; i < MAXN; ++i) {
161+
if (prime(i)) {
162+
isP[i] = true;
163+
plist[pcount++] = i;
164+
}
165+
}
166+
}
167+
168+
class SumAndProductPuzzle {
169+
public:
170+
long long getSum(int A, int B) {
171+
// Editorial http://apps.topcoder.com/wiki/display/tc/TCO+2014+Round+2B
172+
// To prove each S either has no solution, or only one solution which is (1, S - 1)
173+
// Say there's another solution (N, S - N), then 1 + N * (S - N) - 1 is composite.
174+
// And N + S - N - 1 is also composite which contradicts the assumption that N * (S - N)
175+
// has only one factorizations (a, b) that a * b - 1 is composite.
176+
initprime();
177+
FOR(i,A,B) valid[i] = !isP[i - 1];
178+
FOR(x,2,(int)sqrt(B)) {
179+
FOR(y,x,B/x) {
180+
if (!isP[x + y - 1]) valid[x * y + 1] = false;
181+
}
182+
}
183+
LL res = 0;
184+
FOR(i,max(A,3),B) if (valid[i]) res += i;
185+
return res;
186+
}
187+
};
188+
// BEGIN CUT HERE
189+
int main() {
190+
{
191+
SumAndProductPuzzle theObject;
192+
eq(0, theObject.getSum(30, 33),33LL);
193+
}
194+
{
195+
SumAndProductPuzzle theObject;
196+
eq(1, theObject.getSum(8, 11),19LL);
197+
}
198+
{
199+
SumAndProductPuzzle theObject;
200+
eq(2, theObject.getSum(40, 43),0LL);
201+
}
202+
{
203+
SumAndProductPuzzle theObject;
204+
eq(3, theObject.getSum(47, 74),168LL);
205+
}
206+
{
207+
SumAndProductPuzzle theObject;
208+
eq(4, theObject.getSum(1, 2),0LL);
209+
}
210+
{
211+
SumAndProductPuzzle theObject;
212+
eq(4, theObject.getSum(1, 50000),67227724LL);
213+
}
214+
{
215+
SumAndProductPuzzle theObject;
216+
eq(4, theObject.getSum(1, 500000),5051859774LL);
217+
}
218+
return 0;
219+
}
220+
// END CUT HERE
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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><p>
2+
Consider the following story.
3+
</p>
4+
5+
<p>
6+
STORY STARTS HERE.
7+
</p>
8+
9+
<p>
10+
There were three mathematicians: Susan, Priscilla, and Bob.
11+
Bob picked two positive integers x and y such that x &lt;= y.
12+
He then told their sum to Susan and their product to Priscilla.
13+
Susan and Priscilla both knew all the facts listed above.
14+
Then, Susan and Priscilla made the following statements:
15+
<ul>
16+
<li>Susan: "I am certain that you cannot determine my number."</li>
17+
<li>Priscilla: "Thanks for telling me that. Now I'm sure that your number is S."</li>
18+
</ul>
19+
</p>
20+
21+
<p>
22+
STORY ENDS HERE.
23+
</p>
24+
25+
<p>
26+
My friends Baska and Olivia are fond of puzzles.
27+
Recently, Baska told Olivia the above story.
28+
When telling the story, Baska used some specific positive integer (for example, 9) instead of S.
29+
Then, she asked Olivia to determine x and y.
30+
Olivia easily came up with the unique solution.
31+
</p>
32+
33+
<p>
34+
Of course, you don't know the integer Baska used instead of S.
35+
Instead, you are given two ints <b>A</b> and <b>B</b>.
36+
Find all S between <b>A</b> and <b>B</b>, inclusive, such that the above discussion between Baska and Olivia could have happened.
37+
Return the sum of all such S.
38+
</p></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>SumAndProductPuzzle</td></tr><tr><td>Method:</td><td>getSum</td></tr><tr><td>Parameters:</td><td>int, int</td></tr><tr><td>Returns:</td><td>long long</td></tr><tr><td>Method signature:</td><td>long long getSum(int A, int B)</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>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>Watch out for overflow. The return value may overflow a 32-bit integer variable.</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>A</b> will be between 1 and 5,000,000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>B</b> will be between <b>A</b> and 5,000,000, inclusive.</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>30</pre></td></tr><tr><td><pre>33</pre></td></tr></table></td></tr><tr><td><pre>Returns: 33</pre></td></tr><tr><td><table><tr><td colspan="2">The only valid S in this range is 33.
39+
The unique pair (x,y) that corresponds to S=33 is (1,32).</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>8</pre></td></tr><tr><td><pre>11</pre></td></tr></table></td></tr><tr><td><pre>Returns: 19</pre></td></tr><tr><td><table><tr><td colspan="2"></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>40</pre></td></tr><tr><td><pre>43</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2">Sometimes the given range doesn't contain any valid S. In such case the correct return value is 0.</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>47</pre></td></tr><tr><td><pre>74</pre></td></tr></table></td></tr><tr><td><pre>Returns: 168</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</pre></td></tr><tr><td><pre>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</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)