|
| 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 |
0 commit comments