1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <cassert>
#include <algorithm>
#include <map>
using namespace std;

typedef long long ll;
const ll MODULO = 1e9 + 7;
const int MAXDIMENSION = 1e4;
typedef map<int, map<int, map<int, map<int, ll> >  > > Flexi4dArray;
//#define DEBUG

class Math
{
public:
	Math()
	{
		InitializeFactorials();
		InitializeModularInversions();
	}

	ll Factorial(int n)
	{
		return factorials[n];
	}

	ll ModularInversionOfGivenFactorial(int n)
	{
		return modularInversionsOfFactorials[n];
	}

	ll GetModularBinomialCoeff(ll n, ll k)
	{
		if (k > n)
			return 0;
		if (k == n || k == 0)
			return 1;
		const ll inv1 = ModularInversionOfGivenFactorial(k);
		const ll inv2 = ModularInversionOfGivenFactorial(n - k);
		const ll fact = Factorial(n);
		const ll result = (((fact * inv1) % MODULO) * inv2) % MODULO;

		return result;
	}
private:
	void InitializeFactorials()
	{
		factorials[0] = factorials[1] = 1;
		for (ll i = 2; i <= MAXDIMENSION; ++i)
			factorials[i] = (factorials[i - 1] * i) % MODULO;
	}

	void InitializeModularInversions()
	{
		for (int n = 1; n <= MAXDIMENSION; ++n)
		{
			ll numberToInvert = factorials[n];
			modularInversionsOfFactorials[n] = GetModularInversion(numberToInvert);

			if ((numberToInvert * modularInversionsOfFactorials[n]) % MODULO != 1)
				throw string("Unexpeceted issue - wrong inversion");
		}
	}

	static ll GetModularInversion(ll a)
	{
		ll x, y;
		ll gcd = GcdExtended(a, MODULO, &x, &y);
		if (gcd != 1)
			throw string("Unexpeced issue - gcd doesn't exist");

		return (x + MODULO) % MODULO;
	}

	static ll GcdExtended(ll a, ll b, ll *x, ll *y)
	{
		if (a == 0)
		{
			*x = 0, *y = 1;
			return b;
		}

		ll x1, y1;
		ll gcd = GcdExtended(b%a, a, &x1, &y1);

		*x = y1 - (b / a) * x1;
		*y = x1;

		return gcd;
	}

	ll factorials[10000 + 1];
	ll modularInversionsOfFactorials[10000 + 1];
};

Math math;

int BinaryStringToInt(const string& s)
{
	int result = 0;
	for (int pow = 0, i = s.size() - 1; i >= 0; --i, ++pow)
		result += (1 << pow) * (s[i] - '0');

	return result;
}

string ToBinaryString(int number, int dimension)
{
	string result;
	while (number > 0)
	{
		if (number % 2 == 0)
			result.push_back('0');
		else
			result.push_back('1');

		number /= 2;
	}
	while (result.size() < dimension)
		result.push_back('0');

	reverse(result.begin(), result.end());
	return result;
}

struct Sphere
{
	Sphere(){}
	Sphere(int r, string c) : radius(r), center(c) {}
	int radius;
	string center;

	int GetCenter() const
	{
		return BinaryStringToInt(center);
	}

	int GetNumberOfDifferences(const string& otherCenter) const
	{
		if (otherCenter.size() != center.size())
			throw string("Wtf");

		int result = 0;
		for (int i = 0; i < center.size(); ++i)
			result += (center[i] != otherCenter[i]);

		return result;
	}
};

typedef vector<Sphere> Spheres;
Spheres CreateSpheres(int r1, string c1, int r2, string c2, int r3, string c3)
{
	return Spheres{ Sphere(r1, c1), Sphere(r2, c2), Sphere(r3, c3) };
}

struct ISolver
{
	virtual ll Solve(int dimension, Spheres spheres) = 0;
};

static int GetNmberOfDifferentBits(int firstNumber, int secondNumber, int dimension)
{
	int result = 0;

	for (int i = 0; i < dimension; i++)
	{
		if (((firstNumber >> i) & 1) != ((secondNumber >> i) & 1))
			result++;
	}

	return result;
}

class SimpleSolver: public ISolver
{
public:
	ll Solve(int dimension, Spheres spheres)
	{
		ll result = 0;
		for (int candidate = 0; candidate < (1 << dimension); ++candidate)
		{
			for (const Sphere& sphere : spheres)
			{
				if (GetNmberOfDifferentBits(candidate, sphere.GetCenter(), dimension) <= sphere.radius)
				{
					result = (result + 1) % MODULO;
					break;
				}
			}
		}

		return result;
	}
	
};


class SmartSolver : public ISolver
{
public:
	ll Solve(int dimension, Spheres spheres)
	{
		this->dimension = dimension;
		ll result = GetSingleSum(spheres, dimension);
		
		for (int i = 0; i < spheres.size(); ++i)
		{
			for (int j = i + 1; j < spheres.size(); ++j)
				result = (result + (MODULO - GetNumberOfCommonStrings(spheres[i], spheres[j]))) % MODULO;
		}

		result = (result + GetNumberOfCommonStrings(spheres[0], spheres[1], spheres[2])) % MODULO;


		return result;
	}
	
	static ll GetNumberOfCommonStrings(const Sphere& firstSphere, const Sphere& secondSphere)
	{
		const int dimension = firstSphere.center.size();
		if (firstSphere.center == secondSphere.center)
		{
			int minRadius = min(firstSphere.radius, secondSphere.radius);
			return GetNumberOfStringWichDiffersOnAtMostGivenBits(minRadius, dimension);
		}
		else
		{
			int numberOfDifferentBits = firstSphere.GetNumberOfDifferences(secondSphere.center);
			ll result = 0;
			if (numberOfDifferentBits > firstSphere.radius + secondSphere.radius)
				return result;

			for (int r1 = 0; r1 <= min(firstSphere.radius, numberOfDifferentBits); r1++)
			{
				for (int r2 = numberOfDifferentBits - r1; r2 <= min(secondSphere.radius, numberOfDifferentBits); ++r2)
				{
					if (r1 + r2 != numberOfDifferentBits)
						continue;
					int minLeftRadius = min(min(firstSphere.radius - r1, secondSphere.radius - r2), dimension - numberOfDifferentBits);
					result += math.GetModularBinomialCoeff(numberOfDifferentBits, r1) 
						* (dimension > numberOfDifferentBits ? GetNumberOfStringWichDiffersOnAtMostGivenBits(minLeftRadius, dimension - numberOfDifferentBits) : 1);
					result %= MODULO;
				}
			}

			return result;
		}
	}

	static ll GetNumberOfCommonStrings(const Sphere& firstSphere, const Sphere& secondSphere, const Sphere& thirdSphere)
	{
		return GetNumberOfCommonStrings(Spheres{ firstSphere, secondSphere, thirdSphere });
	}

	

private:
	static ll GetNumberOfStringWhichDiffersOnGivenBits(int targetBitsDifference, int dimension)
	{
		return math.GetModularBinomialCoeff(dimension, targetBitsDifference);
	}

	static ll GetNumberOfStringWichDiffersOnAtMostGivenBits(int targetBitsDifference, int dimension)
	{
		ll result = 0;
		for (int difference = 0; difference <= targetBitsDifference; ++difference)
			result = (result + GetNumberOfStringWhichDiffersOnGivenBits(difference, dimension)) % MODULO;

		return result;
	}

	static ll GetSingleSum(const Spheres& spheres, int dimension)
	{
		ll result = 0;
		for (const Sphere& s : spheres)
			result = (result + GetNumberOfStringWichDiffersOnAtMostGivenBits(s.radius, dimension)) % MODULO;

		return result;
	}

	static ll GetNumberOfCommonStrings(const Spheres& spheres)
	{
		differenecs = CreateDifferencesVector(spheres);
		staticDim = spheres[0].center.size();
		storedValues.clear();

		return CalculateNumberOfCommons(0, spheres[0].radius, spheres[1].radius, spheres[2].radius);
	}

	static ll CalculateNumberOfCommons(int currentDifference, int r1, int r2, int r3)
	{
		if (r1 < 0 || r2 < 0 || r3 < 0)
			throw string("What");
		map<int, ll>::iterator it = storedValues[currentDifference][r1][r2].find(r3);
		if (it != storedValues[currentDifference][r1][r2].end())
			return it->second;

		if (r1 == 0 && r2 == 0 && r3 == 0 && currentDifference < differenecs.size())
		{
			return storedValues[currentDifference][r1][r2][r3] = 0;
		}

		if (currentDifference == differenecs.size())
		{
			int minRadius = min(r1, min(r2, r3));
			if (minRadius < 0) return 0;
			return GetNumberOfStringWichDiffersOnAtMostGivenBits(minRadius, staticDim - differenecs.size());
		}

		ll res = 0;
		switch (differenecs[currentDifference])
		{
		case 1:
		case 6:
			res = (r3 > 0 ? CalculateNumberOfCommons(currentDifference + 1, r1, r2, r3 - 1) : 0) 
				+ (r1 > 0 && r2 > 0 ?CalculateNumberOfCommons(currentDifference + 1, r1 - 1, r2 - 1, r3) : 0);
			break;
		case 2:
		case 5:
			res = (r1 > 0 && r3 >0 ? CalculateNumberOfCommons(currentDifference + 1, r1 - 1, r2, r3 - 1) : 0) 
				+ (r2 > 0 ? CalculateNumberOfCommons(currentDifference + 1, r1, r2 - 1, r3) : 0);
			break;
		case 3:
		case 4:
			res = (r2 > 0 && r3 > 0 ? CalculateNumberOfCommons(currentDifference + 1, r1, r2 - 1, r3 - 1) : 0)
				+ (r1 > 0 ? CalculateNumberOfCommons(currentDifference + 1, r1 - 1, r2, r3) : 0);
			break;
		}
		res %= MODULO;

		return storedValues[currentDifference][r1][r2][r3] = res;
	}

	static vector<int> CreateDifferencesVector(const Spheres& spheres)
	{
		vector<int> result;
		for (int len = 0; len < spheres[0].center.size(); ++len)
		{
			int diffType = (spheres[0].center[len] - '0') * 4 + (spheres[1].center[len] - '0') * 2
				+ (spheres[2].center[len] - '0');

			if (diffType == 0 || diffType == 7)
				continue;

			result.push_back(diffType);
		}

		return result;
	}

	static vector<int> differenecs;
	static Flexi4dArray storedValues;
	static int staticDim;
	int dimension;
};

vector<int> SmartSolver::differenecs;
Flexi4dArray SmartSolver::storedValues;
int SmartSolver::staticDim;

class AllPosibilitesTester
{
public:
	AllPosibilitesTester(ISolver* s1, ISolver* s2)
		: firstSolver(s1), secondSolver(s2)
	{
	}

	void Test()
	{
		for (int dimension = 1; dimension <= 5; ++dimension)
		{
			vector<int> allPossibleCenters = GenerateAllPossibleCenters(dimension);
			Spheres spheres = GenerateAllPossibleSpheres(allPossibleCenters, dimension);
			for (const Sphere& s1: spheres)
			{
				for (const Sphere& s2 : spheres)
				{
					for (const Sphere& s3 : spheres)
					{
						Spheres input{ s1, s2, s3 };
						ll r1 = firstSolver->Solve(dimension, input);
						ll r2 = secondSolver->Solve(dimension, input);

						if (r1 != r2)
						{
							cout << "Ups! First solver: " << r1 << ", second solver: " << r2 << "\n";
						}
					}
				}
			}
		}
	}

private:
	vector<int> GenerateAllPossibleCenters(int dimension)
	{
		vector<int> result;
		for (int i = 0; i < (1 << dimension); ++i)
			result.push_back(i);

		return result;
	}

	Spheres GenerateAllPossibleSpheres(const vector<int>& allPossibleCenters, int dimension)
	{
		Spheres result;
		for (int d = 0; d <= dimension; ++d)
		{
			for (int center : allPossibleCenters)
				result.push_back(Sphere(d, ToBinaryString(center, dimension)));
		}

		return result;
	}

	ISolver* firstSolver;
	ISolver* secondSolver;
};

ll ComputeSimpleCommon(const Sphere& s1, const Sphere& s2)
{
	const int dim = s1.center.size();
	int result = 0;
	for (int candidate = 0; candidate < (1 << dim); ++candidate)
	{
		if (GetNmberOfDifferentBits(candidate, s1.GetCenter(), dim) <= s1.radius
			&& GetNmberOfDifferentBits(candidate, s2.GetCenter(), dim) <= s2.radius)
			++result;
	}

	return result;
}

ll ComputeSimpleCommon(const Sphere& s1, const Sphere& s2,  const Sphere& s3)
{
	const int dim = s1.center.size();
	int result = 0;
	for (int candidate = 0; candidate < (1 << dim); ++candidate)
	{
		if (GetNmberOfDifferentBits(candidate, s1.GetCenter(), dim) <= s1.radius
			&& GetNmberOfDifferentBits(candidate, s2.GetCenter(), dim) <= s2.radius
			&& GetNmberOfDifferentBits(candidate, s3.GetCenter(), dim) <= s3.radius)
			++result;
	}

	return result;
}

void Debug()
{
	SimpleSolver simpleSolver;
	SmartSolver smartSolver;

	for (int dim = 1; dim <= 9 * 0; ++dim)
	{
		cout << "dim=" << dim << endl;
		for (int i = 0; i < (1 << dim); ++i)
		{
			for (int j = 0; j < (1 << dim); ++j)
			{
				for (int r1 = 0; r1 <= dim; ++r1)
				{
					for (int r2 = 0; r2 <= dim; ++r2)
					{
						Sphere s1(r1, ToBinaryString(i, dim));
						Sphere s2(r2, ToBinaryString(j, dim));
						ll smartCommon = SmartSolver::GetNumberOfCommonStrings(s1, s2);
						ll simpleCommon = ComputeSimpleCommon(s1, s2);

						if (smartCommon != simpleCommon)
						{
							cout << "Ups2 " << smartCommon << " != " << simpleCommon << "\n";
						}
					}
				}
			}
		}
	}

	for (int dim = 1; dim <= 5 * 0; ++dim)
	{
		for (int i = 0; i < (1 << dim); ++i)
		{
			for (int j = 0; j < (1 << dim); ++j)
			{
				for (int k = 0; k < (1 << dim); ++k)
				{
					for (int r1 = 0; r1 <= dim; ++r1)
					{
						for (int r2 = 0; r2 <= dim; ++r2)
						{
							for (int r3 = 0; r3 <= dim; ++r3)
							{
								Sphere s1(r1, ToBinaryString(i, dim));
								Sphere s2(r2, ToBinaryString(j, dim));
								Sphere s3(r3, ToBinaryString(k, dim));
								ll smartCommon = SmartSolver::GetNumberOfCommonStrings(s1, s2, s3);
								ll simpleCommon = ComputeSimpleCommon(s1, s2, s3);

								if (smartCommon != simpleCommon)
								{
									cout << "Ups3 " << smartCommon << " != " << simpleCommon << "\n";
								}
							}
						}
					}
				}
			}
		}
	}

	assert(simpleSolver.Solve(3, CreateSpheres(1, "000", 1, "100", 0, "111")) == 7);
	assert(simpleSolver.Solve(5, CreateSpheres(2, "10110", 0, "11010", 1, "00000")) == 19);
	assert(math.Factorial(1) == 1);
	assert(math.Factorial(4) == 24);
	assert(math.Factorial(10) == 3628800);
	for (int n = 1; n <= 100; ++n)
		assert(math.GetModularBinomialCoeff(n, 1) == n);

	for (int n = 1; n <= 100; ++n)
		assert(math.GetModularBinomialCoeff(n, n) == 1);

	for (int n = 2; n <= 100; ++n)
		assert(math.GetModularBinomialCoeff(n, 2) == n * (n-1) / 2);

	AllPosibilitesTester allPosibilitesTester(&simpleSolver, &smartSolver);
	allPosibilitesTester.Test();

	cout << "Debug done\n";
}

struct Input
{
	int dimension;
	Spheres spheres;
};

Input ReadInput()
{
	Input input;
	cin >> input.dimension;
	for (int i = 0; i < 3; ++i)
	{
		Sphere sphere;
		cin >> sphere.radius;
		cin >> sphere.center;
		input.spheres.push_back(sphere);
	}

	return input;
}
int main()
{
	ios::sync_with_stdio(false);
#ifdef DEBUG
	Debug();
#endif
	SmartSolver smartSolver;
	Input input = ReadInput();

	cout << smartSolver.Solve(input.dimension, input.spheres) << "\n";
#ifdef DEBUG
	system("pause");
#endif
}