| Task: | Bittijono |
| Sender: | Yytsi |
| Submission time: | 2017-10-12 19:48:53 +0300 |
| Language: | C++ |
| Status: | READY |
| Result: | 100 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 7 |
| #2 | ACCEPTED | 15 |
| #3 | ACCEPTED | 27 |
| #4 | ACCEPTED | 51 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.04 s | 1 | details |
| #2 | ACCEPTED | 0.05 s | 1 | details |
| #3 | ACCEPTED | 0.06 s | 1 | details |
| #4 | ACCEPTED | 0.06 s | 1 | details |
| #5 | ACCEPTED | 0.05 s | 1 | details |
| #6 | ACCEPTED | 0.05 s | 1 | details |
| #7 | ACCEPTED | 0.07 s | 1 | details |
| #8 | ACCEPTED | 0.06 s | 1 | details |
| #9 | ACCEPTED | 0.05 s | 1 | details |
| #10 | ACCEPTED | 0.05 s | 1 | details |
| #11 | ACCEPTED | 0.05 s | 2 | details |
| #12 | ACCEPTED | 0.06 s | 2 | details |
| #13 | ACCEPTED | 0.05 s | 2 | details |
| #14 | ACCEPTED | 0.08 s | 2 | details |
| #15 | ACCEPTED | 0.06 s | 2 | details |
| #16 | ACCEPTED | 0.08 s | 2 | details |
| #17 | ACCEPTED | 0.07 s | 2 | details |
| #18 | ACCEPTED | 0.07 s | 2 | details |
| #19 | ACCEPTED | 0.07 s | 2 | details |
| #20 | ACCEPTED | 0.06 s | 2 | details |
| #21 | ACCEPTED | 0.06 s | 3 | details |
| #22 | ACCEPTED | 0.13 s | 3 | details |
| #23 | ACCEPTED | 0.16 s | 3 | details |
| #24 | ACCEPTED | 0.05 s | 3 | details |
| #25 | ACCEPTED | 0.06 s | 3 | details |
| #26 | ACCEPTED | 0.05 s | 3 | details |
| #27 | ACCEPTED | 0.05 s | 3 | details |
| #28 | ACCEPTED | 0.18 s | 3 | details |
| #29 | ACCEPTED | 0.06 s | 3 | details |
| #30 | ACCEPTED | 0.05 s | 3 | details |
| #31 | ACCEPTED | 0.34 s | 4 | details |
| #32 | ACCEPTED | 0.58 s | 4 | details |
| #33 | ACCEPTED | 0.57 s | 4 | details |
| #34 | ACCEPTED | 0.48 s | 4 | details |
| #35 | ACCEPTED | 0.85 s | 4 | details |
| #36 | ACCEPTED | 0.44 s | 4 | details |
| #37 | ACCEPTED | 0.66 s | 4 | details |
| #38 | ACCEPTED | 0.06 s | 4 | details |
| #39 | ACCEPTED | 0.45 s | 4 | details |
| #40 | ACCEPTED | 0.08 s | 4 | details |
Compiler report
input/code.cpp: In function 'int main()':
input/code.cpp:181:25: warning: statement has no effect [-Wunused-value]
if (res == "fail") 0;//cout << "Length of " << j << " failed!\n";
^
input/code.cpp:163:6: warning: unused variable 'high' [-Wunused-variable]
int high = len + 2;
^Code
/*
Written by, Tuukka Yildirim.
input: n.
I blindly followed a false idea that I came up with, which prolonged the
solving of this problem. Always doubt your ideas that aren't deeply thought ;-)
A binary string of length <s> will have the most unique substrings when filled with
consecutive ones and zeros. For example, "10101" is the max for <s> = 5.
Based on this fact, I thought that the shortest binary string <b> with exactly <n> unique
substrings, will not be found on a region, that has higher amount of maximum unique substrings.
For example, "10101" contains 19 unique substrings, the highest for <s> = 5. I thought that if
I were to find the shortest binary string containing 5 unique substrings, it couldn't possibly
be at <s> = 4 or over, when in fact it could be found at <s> = a high number (in this example,
it's actually not true, but generally it is).
When I dumped the idea, I constructed a Monte Carlo algorithm to solve the problem.
Let's call the solution <b>, that is the shortest binary string containing exactly
<n> unique substrings. NOTE! <b> is not a constant string, but instead many strings
at the same time (for most of the time).
First, I try to find the solution from the lowest length, from which <b> can be found.
I do this up to a length of 32 inclusive.
I have a string array <chains> that contain templates to be tested, before shuffling.
For every length, I take a string <s>, that is one of the strings in <chains> on the
first 8 iterations, and otherwise a shuffled string based on the modifications done to it.
I take <s> and start flipping every bit starting from the second bit. At each individual
flip, I calculate the fitness of <s> with the following formula: abs(distinctSubstrings(s) - n)
The fitness yields how close it is to the binary string <b>; 0 meaning it's <b>.
After testing, I flip the bit that led to the best fitness (lowest value). I do this, as long as
the fitness keeps getting better, or the solution gets found. If I encounter a string, that doesn't
yield any improvement, I shuffle the string with C++'s std::random_shuffle, that can
be found at <algorithm>s. I do this almost 21000 times, which seems to be enough.
NOTE! I never shuffle the first index, nor do I flip it, since it doesn't matter whether the first
bit is 0 or 1. They are basically the same thing, when you think about it.
A deterministic algorithm I thought about concerned inverting the function that calculates the amount
of unique substrings a binary string has. However, this seemed easier. Gratz to everyone who solved
this with a fast and deterministic algorithm! :)
*/
#include <iostream>
#include <string>
#include <algorithm>
#define T 8
using namespace std;
int n;
string chains[T] = {"101010101010101010101010101010101010101010101",
"110100100100010011010010010010010010010010010",
"111111111111111111111111111111111111111111111",
"100000100001000100100001000010000100001000010",
"100010001001011100111110111111011001111101111",
"100101000111010010110101001010010100011101001",
"110110101100101010101010010111011010110010101",
"101011010110100110010011010011010110101101001"};
int bounds[27] = {1, 3, 6, 11, 19, 32, 53, 87, 142, 231, 375, 608, 985, 1595, 2582, 4179,
6763, 10944, 17709, 28655, 46366, 75023, 121391, 196416, 317809, 514227, 832038};
int numberOfDistinct(const string& str)
{
int ones = 0;
int zeros = 0;
int distinct = 0;
for (size_t i = 0; i < str.length(); i++)
{
char bit = str[i];
bool isOne = bit == '1';
int newSum = 1 + distinct;
distinct = distinct - (isOne ? ones : zeros) + newSum;
if (isOne) ones = newSum;
else zeros = newSum;
}
return distinct;
}
int binSizeLowerBound(int n)
{
for (int i = 26; i != 0; i--)
{
int bound = bounds[i];
if (bound < n)
{
return i + 1;
}
}
return 1;
}
string findBitstring(string b, int tries)
{
size_t len = b.size();
for (int test = 0; test < tries; test++)
{
int maxFitness = 123456879, fitIndex = -1;
while (true)
{
bool foundImprovement = false;
for (size_t i = 1; i < len; i++)
{
char bit = b[i];
char rev = (bit == '1') ? '0' : '1';
b[i] = rev;
int distinctSubs = numberOfDistinct(b);
//cout << b << " : " << distinctSubs << "\n";
int fitness = abs(distinctSubs - n);
if (fitness == 0)
{
// Solution found.
return b;
}
if (fitness < maxFitness)
{
maxFitness = fitness;
fitIndex = (int)i;
foundImprovement = true;
}
b[i] = bit;
}
if (foundImprovement) b[fitIndex] = (b[fitIndex] == '1') ? '0' : '1';
else
{
//cout << "No improvement found :-( shuffling\n";
if (test < T) b = chains[test].substr(0, len);
else random_shuffle(b.begin() + 1, b.end());
break;
}
}
}
return "fail";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
bool found = false;
string s = "10101010101010101010101010101010101010101010";
int len = binSizeLowerBound(n);
int low = len + 1;
int high = len + 2;
for (int i = 0; i < 27; i++)
if (bounds[i] == n) found = true;
bool foundAns = false;
if (n == 1) cout << "1";
else if (found) cout << s.substr(0, low);
else
{
for (int i = 0; i < 1; i++)
{
string chain = s;
for (int j = low; j < 33; j++)
{
string sub = chain.substr(0, j);
string res = findBitstring(sub, 21000);
if (res == "fail") 0;//cout << "Length of " << j << " failed!\n";
else
{
// Found an answer....
cout << res << "\n";
foundAns = true;
break;
}
}
if (foundAns) break;
}
}
return 0;
}Test details
Test 1
Group: 1
Verdict: ACCEPTED
| input |
|---|
| 1 |
| correct output |
|---|
| 1 |
| user output |
|---|
| 1 |
Test 2
Group: 1
Verdict: ACCEPTED
| input |
|---|
| 2 |
| correct output |
|---|
| 11 |
| user output |
|---|
| 11 |
Test 3
Group: 1
Verdict: ACCEPTED
| input |
|---|
| 3 |
| correct output |
|---|
| 10 |
| user output |
|---|
| 10 |
Test 4
Group: 1
Verdict: ACCEPTED
| input |
|---|
| 4 |
| correct output |
|---|
| 1111 |
| user output |
|---|
| 1111 |
Test 5
Group: 1
Verdict: ACCEPTED
| input |
|---|
| 5 |
| correct output |
|---|
| 110 |
| user output |
|---|
| 100 |
Test 6
Group: 1
Verdict: ACCEPTED
| input |
|---|
| 6 |
| correct output |
|---|
| 101 |
| user output |
|---|
| 101 |
Test 7
Group: 1
Verdict: ACCEPTED
| input |
|---|
| 7 |
| correct output |
|---|
| 1110 |
| user output |
|---|
| 1110 |
Test 8
Group: 1
Verdict: ACCEPTED
| input |
|---|
| 8 |
| correct output |
|---|
| 1100 |
| user output |
|---|
| 1100 |
Test 9
Group: 1
Verdict: ACCEPTED
| input |
|---|
| 9 |
| correct output |
|---|
| 1101 |
| user output |
|---|
| 1011 |
Test 10
Group: 1
Verdict: ACCEPTED
| input |
|---|
| 10 |
| correct output |
|---|
| 1001 |
| user output |
|---|
| 1001 |
Test 11
Group: 2
Verdict: ACCEPTED
| input |
|---|
| 38 |
| correct output |
|---|
| 1101011 |
| user output |
|---|
| 1101011 |
Test 12
Group: 2
Verdict: ACCEPTED
| input |
|---|
| 13 |
| correct output |
|---|
| 11011 |
| user output |
|---|
| 11011 |
Test 13
Group: 2
Verdict: ACCEPTED
| input |
|---|
| 90 |
| correct output |
|---|
| 111001010 |
| user output |
|---|
| 101011000 |
Test 14
Group: 2
Verdict: ACCEPTED
| input |
|---|
| 25 |
| correct output |
|---|
| 110010 |
| user output |
|---|
| 110010 |
Test 15
Group: 2
Verdict: ACCEPTED
| input |
|---|
| 82 |
| correct output |
|---|
| 111001101 |
| user output |
|---|
| 101100111 |
Test 16
Group: 2
Verdict: ACCEPTED
| input |
|---|
| 94 |
| correct output |
|---|
| 1100011110 |
| user output |
|---|
| 1100011110 |
Test 17
Group: 2
Verdict: ACCEPTED
| input |
|---|
| 100 |
| correct output |
|---|
| 1111001001 |
| user output |
|---|
| 1111001001 |
Test 18
Group: 2
Verdict: ACCEPTED
| input |
|---|
| 99 |
| correct output |
|---|
| 110010010 |
| user output |
|---|
| 101101100 |
Test 19
Group: 2
Verdict: ACCEPTED
| input |
|---|
| 98 |
| correct output |
|---|
| 110110010 |
| user output |
|---|
| 101000110 |
Test 20
Group: 2
Verdict: ACCEPTED
| input |
|---|
| 92 |
| correct output |
|---|
| 100110001 |
| user output |
|---|
| 100110001 |
Test 21
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 1666 |
| correct output |
|---|
| 101101100100101 |
| user output |
|---|
| 101001001101101 |
Test 22
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 897 |
| correct output |
|---|
| 11101001101010 |
| user output |
|---|
| 11010010111010 |
Test 23
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 4466 |
| correct output |
|---|
| 111101010110100101 |
| user output |
|---|
| 101001001110001011 |
Test 24
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 4240 |
| correct output |
|---|
| 11011001011010101 |
| user output |
|---|
| 11011001011010101 |
Test 25
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 3089 |
| correct output |
|---|
| 1011001010100101 |
| user output |
|---|
| 1010010101001101 |
Test 26
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 4697 |
| correct output |
|---|
| 11010101101010110 |
| user output |
|---|
| 11010101101010110 |
Test 27
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 4608 |
| correct output |
|---|
| 11010110101001010 |
| user output |
|---|
| 11010110101001010 |
Test 28
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 4625 |
| correct output |
|---|
| 111011001100101001 |
| user output |
|---|
| 110110110101011000 |
Test 29
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 4611 |
| correct output |
|---|
| 11010101010101100 |
| user output |
|---|
| 11010100101001010 |
Test 30
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 4917 |
| correct output |
|---|
| 10110100101010110 |
| user output |
|---|
| 10010101011010010 |
Test 31
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 178555 |
| correct output |
|---|
| 1011010110110101010110110 |
| user output |
|---|
| 1001001010101001001010010 |
Test 32
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 864856 |
| correct output |
|---|
| 10111010110110100100101010010 |
| user output |
|---|
| 10110110011010101001110011010 |
Test 33
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 112146 |
| correct output |
|---|
| 1101110101011001100100110 |
| user output |
|---|
| 1100101000101000110100101 |
Test 34
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 741124 |
| correct output |
|---|
| 1011010011010101100101011010 |
| user output |
|---|
| 1011010011010101100101011010 |
Test 35
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 511902 |
| correct output |
|---|
| 1011010100011010100101001110 |
| user output |
|---|
| 1010100100101010100001101001 |
Test 36
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 920019 |
| correct output |
|---|
| 11100100101101010101001101010 |
| user output |
|---|
| 11001011010100101100100101101 |
Test 37
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 933943 |
| correct output |
|---|
| 10101011010100100110100111001 |
| user output |
|---|
| 10011100101100100101011010101 |
Test 38
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 973410 |
| correct output |
|---|
| 1011010101011010101010101001 |
| user output |
|---|
| 1011010101011010101010101001 |
Test 39
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 954943 |
| correct output |
|---|
| 10110110010011010100100110101 |
| user output |
|---|
| 10100100110101010111010010110 |
Test 40
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 911674 |
| correct output |
|---|
| 1010110010110101010101010110 |
| user output |
|---|
| 1010110010110101010101010110 |
