Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import math,itertools as it
M = 256
while 1:
N = int(raw_input())
if N == 0: break
H = 1e10
I = map(int,raw_input().split())
for S,A,C in it.product(range(16),repeat=3):
R = S
O = [0]*M
for i in I:
R = (A*R+C)%M
O[(i+R)%M] += 1
tmp = -sum(1.0*x/N*math.log(1.0*x/N,2) for x in O if x > 0)
if tmp < H:
H = tmp
ans = [S,A,C]
print " ".join(map(str,ans)) | PYTHON |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!
#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
const int M = 256;
int N;
while (cin >> N, N) {
V I(N);
rep(i, N) cin >> I[i];
double mi = 1e9;
int S, A, C;
rep(s, 15 + 1) {
rep(a, 15 + 1) {
rep(c, 15 + 1) {
int r = s;
map<int, int> mp; // O(i), #occurence
rep(i, N) {
r = (a * r + c) % M;
mp[(I[i] + r) % M]++;
}
double h = 0.;
for (auto p : mp) {
h += - ((p.second * 1.) / N) * log((p.second * 1.) / N);
}
if (h + 1e-10 < mi) {
mi = h;
S = s, A = a, C = c;
}
}
}
}
cout << S << " " << A << " " << C << endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
const int MAX_N = 260;
const int MOD = 256;
const double EPS = 1e-8;
int N;
int init(vector<int> &R, int &s, int &a, int &c, int n){
if(n <= 0) return R[0] = s;
return R[n] = (a * init(R, s, a, c, n - 1) + c) % MOD;
}
double check(vector<int> &O){
double res = 0.0;
map<int, int> M;
REP(i, N) M[O[i]] += 1;
for(pair<const int, int> p : M) res -= (double)p.second / N * log((double)p.second / N);
return res;
}
int main() {
while(cin >>N && N){
vector<int> I(N); REP(i, N) cin >>I[i];
int S = 0, A = 0, C = 0;
double H = 1e9;
REP(s, 16){
REP(a, 16){
REP(c, 16){
vector<int> R(N + 10), O(N);
init(R, s, a, c, N);
REP(i, N) O[i] = (I[i] + R[i + 1]) % MOD;
double h = check(O);
if(h + EPS < H) { S = s; A = a; C = c; H = h; }
}
}
}
cout <<S <<" " <<A <<" " <<C <<endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define M 256
#define EPS 1e-9
int main()
{
int N;
while(cin >> N, N){
int I[256];
for(int i = 0; i < N; i++) cin >> I[i];
double H = 1000000.0;
int S, A, C;
for(int s = 0; s <= 15; s++){
for(int a = 0; a <= 15; a++){
for(int c = 0; c <= 15; c++){
int R[256]; R[0] = s;
int x[256] = {0};
for(int i = 0; i < N; i++){
R[i+1] = (a*R[i] + c) % M;
x[(I[i] + R[i+1])%M]++;
}
double h = 0.0;
for(int i = 0; i < 256; i++){
if(x[i] == 0) continue;
h -= (double)x[i] / N * log((double)x[i] / N);
}
if(h + EPS < H) H = h, S = s, A = a, C = c;
}
}
}
cout << S << " " << A << " " << C << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import math
M = 256
while 1:
N = int(raw_input())
if N == 0: break
H = 1e10
l = map(int,raw_input().split())
ans = [0,0,0]
for S in range(16):
for A in range(16):
for C in range(16):
R = S
O = [0]*N
for i in range(N):
R = (A*R+C)%M
O[i] = (l[i]+R)%M
L = [0]*256
for i in O: L[i] += 1
tmp = -sum(float(i)/N*math.log(float(i)/N,2) for i in L if i != 0)
if tmp < H:
H = tmp
ans = [S,A,C]
print " ".join(map(str,ans)) | PYTHON |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<iostream>
#include<cmath>
#define EPS 1e-8
using namespace std;
double getEntropy(int s,int a,int c,int *l,int n){
double sum[256]={0};
double res=0;
int r=s;
for(int i=0;i<n;i++){
r=(a*r + c)&255;
sum[(r + l[i])&255]++;
}
for(int i=0;i<256;i++){
//cout << "sum["<<i<<"]:"<<sum[i] << endl;
if(sum[i]==0)continue;
res-=sum[i]/n * log(sum[i]/n);
//cout << "res:" << res << endl;
}
return res;
}
int main(){
int n;
while(cin>>n,n){
int l[n];
double mn=100000000;
int S,A,C;
for(int i=0;i<n;i++)cin>>l[i];
for(int s=0;s<16;s++){
for(int a=0;a<16;a++){
for(int c=0;c<16;c++){
double tmp=getEntropy(s,a,c,l,n);
if(mn-tmp>EPS){
mn=tmp;
S=s;
A=a;
C=c;
//cout << "S:" << S << " A:" << A << " C:" << C << endl;
//cout << "res:" << mn << endl;
}
}
}
}
cout << S << " " << A << " " << C << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE
#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
int n;
double calc(vector<int> &cnt){
double res = 0;
for (double x : cnt){
if(x) res -= x / n*log(x / n);
}
return res;
}
tuple<int, int, int> bf(vector<int> &v){
double opt = 1e9;
auto ans = MT(-1, -1, -1);
rep(s, 16) rep(a, 16) rep(c, 16){
vector<int> cnt(256);
int r = s;
rep(i, v.size()){
r = (a*r + c)&255;
++cnt[(v[i] + r)&255];
}
double tmp = calc(cnt);
if (tmp + 1e-9 < opt){
ans = MT(s, a, c);
opt = tmp;
}
}
return ans;
}
int main(){
while (cin >> n, n){
vector<int> v(n);
rep(i, n) cin >> v[i];
int s, a, c;
tie(s, a, c) = bf(v);
cout << s << ' ' << a << ' ' << c << '\n';
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import math
M = 256
while 1:
N = int(raw_input())
if N == 0: break
H = 1e10
l = map(int,raw_input().split())
for S in range(16):
for A in range(16):
for C in range(16):
R = S
O = [0]*M
for i in range(N):
R = (A*R+C)%M
O[(l[i]+R)%M] += 1
tmp = -sum(1.0*x/N*math.log(1.0*x/N,2) for x in O if x != 0)
if tmp < H:
H = tmp
ans = [S,A,C]
print " ".join(map(str,ans)) | PYTHON |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import math
M = 256
while 1:
N = int(raw_input())
if N == 0: break
H = 1e10
l = map(int,raw_input().split())
ans = [0,0,0]
for S in range(16):
for A in range(16):
for C in range(16):
R = S
O = [0]*256
for i in range(N):
R = (A*R+C)%M
O[(l[i]+R)%M] += 1
tmp = -sum(float(x)/N*math.log(float(x)/N,2) for x in O if x != 0)
if tmp < H:
H = tmp
ans = [S,A,C]
print " ".join(map(str,ans)) | PYTHON |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define EPS 1e-9
#define M 256
#define INF 1e9
int main(){
int N;
while(cin >> N, N > 0){
int I[N+1],R[N+1],O[N+1];
for(int i = 0 ; i < N ; i++){
cin >> I[i];
}
int S,A,C;
double ans = INF;
for(int s = 0 ; s <= 15 ; s++){
for(int a = 0 ; a <= 15 ; a++){
for(int c = 0 ; c <= 15 ; c++){
int x[M] = {0};
R[0] = s;
for(int i = 1 ; i <= N ;i++){
R[i] = (a*R[i-1]+c) % M;
O[i] = (I[i-1]+R[i]) % M;
x[O[i]]++;
}
double H = 0;
for(int i = 0 ; i < M ; i++){
double p = (double)x[i]/N;
if(p > 0){
H -= p*log(p);
}
}
if(H+EPS < ans){
ans = H;
S = s; A = a; C = c;
}
}
}
}
cout << S << " " << A << " " << C << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | //57
#include<iostream>
#include<cmath>
using namespace std;
int main(){
for(int n;cin>>n,n;){
int l[256];
for(int i=0;i<n;i++){
cin>>l[i];
}
double me=1<<30;
int ms,ma,mc;
for(int s=0;s<16;s++){
for(int a=0;a<16;a++){
for(int c=0;c<16;c++){
int oa[256]={};
int r=s;
for(int i=0;i<n;i++){
r=(r*a+c)%256;
oa[(r+l[i])%256]++;
}
double ce=0;
for(int i=0;i<256;i++){
if(oa[i]){
ce-=oa[i]*1./n*log(oa[i]*1./n);
}
}
if(ce<me-1e-9){
me=ce;
ms=s;
ma=a;
mc=c;
}
}
}
}
cout<<ms<<' '<<ma<<' '<<mc<<endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<iostream>
#include<cmath>
using namespace std;
struct S{
double min;
int s, a, c;
};
int main(){
int N, l[257],r[257],o[257],x[257];
int s, a, c;
double H;
S ans;
while (1){
cin >> N;
if (N == 0) break;
for (int i = 1; i <= N; i++){
cin >> l[i];
}
for (s = 0; s <= 15; s++){
for (a = 0; a <= 15; a++){
for (c = 0; c <= 15; c++){
r[0] = s;
for (int i = 1; i <= N; i++){
r[i] = (a*r[i - 1] + c) % 256;
}
for (int i = 1; i <= N; i++){
o[i] = (l[i] + r[i]) % 256;
}
for (int i = 0; i < 256; i++) {
x[i] = 0;
}
for (int i = 1; i <= N; i++){
x[o[i]]++;
}
H = 0;
for (int i = 0; i < 256; i++){
if(x[i]!=0) H += ((double)x[i] / N)*log((double)x[i] / N);
}
H *= -1;
if (s == 0 && a == 0 && c == 0 || ans.min>H+1e-9){
ans.min = H;
ans.s = s, ans.a = a, ans.c = c;
}
}
}
}
cout << ans.s << " " << ans.a << " " << ans.c << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import java.util.*;
import static java.lang.System.*;
public class Main {
Scanner sc = new Scanner(in);
void run() {
int n;
int[][][][] R = new int[16][16][16][258];
for (int s = 0; s < 16; s++) {
for (int a = 0; a < 16; a++) {
for (int c = 0; c < 16; c++) {
R[s][a][c][0] = s;
for (int i = 1; i < 258; i++) {
R[s][a][c][i] = (a*R[s][a][c][i-1] + c) % 256;
}
}
}
}
while (true) {
n = sc.nextInt();
if (n == 0) break;
int[] I = new int[n];
for (int i = 0; i < n; i++)
I[i] = sc.nextInt();
int[] O = new int[n];
int anss, ansa, ansc;
anss = ansa = ansc = 0;
double min = Double.MAX_VALUE;
int[] num = new int[257];
for (int s = 0; s < 16; s++) {
for (int a = 0; a < 16; a++) {
for (int c = 0; c < 16; c++) {
for (int i = 0; i < n; i++) {
O[i] = (I[i] + R[s][a][c][i+1]) % 256;
}
Arrays.fill(num, 0);
for (int i : O)
num[i]++;
double h = 0;
for (int i : num) {
if (i > 0) {
h -= (double)i/n*Math.log10((double)i/n);
}
}
if (h+1E-6 < min) {
min = h;
// out.printf("%.20f\n",min);
anss = s;
ansa = a;
ansc = c;
}
}
}
}
out.printf("%d %d %d\n", anss, ansa, ansc);
}
}
public static void main(String[] args) {
new Main().run();
}
} | JAVA |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int n;
int I[555];
int S,A,C;
const int mod = 256;
int r[555];
int R(int i)
{
if(i==0)return r[i]=S;
return r[i]=(A*R(i-1)+C)%mod;
}
int main(void)
{
typedef long double ldouble;
for(;;) {
scanf("%d",&n); if(!n) break;
for(int i = 0; i < n; i++) {
scanf("%d", I+i);
}
ldouble res = 1<<21;
int s,a,c;
int o[256];
for(int i = 0; i < 16; i++) {
for(int j = 0; j < 16; j++) {
for(int k = 0; k < 16; k++) {
S = i; A = j; C = k;
for(int ii = 0; ii < 256; ii++) {
o[ii] = 0;
}
R(n);
for(int ii = 0; ii < n; ii++) {
o[(I[ii]+r[ii+1])%mod]++;
}
ldouble h = 0;
for(int ii = 0; ii < 256; ii++) {
if(o[ii]) {
h += ((ldouble(o[ii])/n)*log(ldouble(o[ii])/n));
}
}
h = -h;
//if(S==8&&A==7&&C==14)printf("v:%.10Lf\n",h);
if( res > h+1e-8 ) {
//printf("%.10Lf %.10Lf %d %d %d\n",res,h,S,A,C);
res = h;
s = S;
a = A;
c = C;
}
}
}
}
printf("%d %d %d\n",s,a,c);
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<iostream>
#include<math.h>
using namespace std;
int in[300], R[300], out[300];
const int M=256;
int cnt[300];
const double eps=1e-9;
int s, a, c;
void hoge(int N){
for(int i=1; i<=N; i++) cin>> in[i];
double h=10000000;
for(int S=0; S<=15; S++){
R[0]=S;
for(int A=0; A<=15; A++){
for(int C=0; C<=15; C++){
for(int i=1; i<=N; i++){
(R[i]=A*R[i-1]+C)%=M;
}
for(int i=1; i<=N; i++){
(out[i]=in[i]+R[i])%=M;
}
for(int i=0; i<=M; i++) cnt[i]=0;
for(int i=1; i<=N; i++) cnt[out[i]]++;
double H=0;
for(int i=0; i<=M; i++){
if(cnt[i]) H-=((1.*cnt[i]/N)*log(1.*cnt[i]/N));
}
if(H+eps<h){
h=H;
s=S;
a=A;
c=C;
}
}
}
}
cout<< s<< " "<< a<< " "<< c<< endl;
}
int main(){
while(1){
int N;
cin>> N;
if(!N) break;
hoge(N);
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <complex>
#include <set>
#include <cmath>
using namespace std;
int I[256];
int N;
double func(int S,int A,int C){
int R[256] = {};
R[0] = S;
for(int i = 1 ; i < 256 ; i++){
R[i] = (A*R[i-1]+C) % 256;
}
int Alpha[256] = {};
for(int i = 0 ; i < N ; i++){
int O = (I[i]+R[i+1]) % 256;
Alpha[O]++;
}
double answer = 0;
for(int i = 0 ; i < 256 ; i++){
if( Alpha[i] == 0 ) continue;
answer += Alpha[i] * log(1.*Alpha[i]/N);
}
return - answer / N;
}
int main(){
while( cin >> N && N){
for(int i = 0 ; i < N ; i++) cin >> I[i];
double ans = 1e15;
int SS,AA,CC;
for(int S = 0 ; S < 16 ; S++){
for(int A = 0 ; A < 16 ; A++){
for(int C = 0 ; C < 16 ; C++){
double calc = func(S,A,C);
if( calc < ans - 1e-10 ){
ans = calc;
SS = S;
AA = A;
CC = C;
}
}
}
}
//cout << func(8,7,14) << endl;
cout << SS << " " << AA << " " << CC << endl;
//printf("%.10lf\n",ans);
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
while (cin >> n, n) {
vector<int> l(n);
for (int i = 0; i < n; i++) {
cin >> l[i];
}
const int mod = 256;
int anss = 16, ansa = 16, ansc = 16;
double minh = 1e9;
for (int s = 0; s < 16; s++) {
for (int a = 0; a < 16; a++) {
for (int c = 0; c < 16; c++) {
int r = s, cnt[256] = {};
for (int i = 0; i < n; i++) {
r = (a * r + c) % mod;
cnt[(l[i] + r) % mod]++;
}
double h = 0.0;
for (int i = 0; i < 256; i++) {
if (!cnt[i]) continue;
h += cnt[i] * log((double)cnt[i] / n);
}
h = -h / n;
if (h - minh < -1e-10) {
minh = h;
anss = s, ansa = a, ansc = c;
}
}
}
}
cout << anss << " " << ansa << " " << ansc << endl;
}
return 0;
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
double entropy(int N, const vector<int> &x) {
double H = 0;
for (int i=0; i<256; ++i) {
if (x[i] != 0) {
H -= (double)x[i] / N * log((double)x[i] / N);
}
}
return H;
}
int main() {
ios::sync_with_stdio(false);
int N;
while (cin >> N && N) {
vector<int> I(N+1);
for (int i=1; i<=N; ++i) cin >> I[i];
int M = 256;
double Hmin = 999999;
vector<int> ans(3);
for (int S=0; S<=15; ++S) {
for (int A=0; A<=15; ++A) {
for (int C=0; C<=15; ++C) {
vector<int> x(256, 0);
int R = S;
int O;
for (int i=1; i<=N; ++i) {
R = (A * R + C) % M;
O = (I[i] + R) % M;
x[O] ++;
}
double H = entropy(N, x);
if (H + 1e-8 < Hmin) {
Hmin = H;
ans[0] = S;
ans[1] = A;
ans[2] = C;
}
}
}
}
cout << ans[0] << " " << ans[1] << " " << ans[2] << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int n;
vector<int> make_R(int s,int a,int c) {
vector<int> R(n+1);
R[0] = s;
for(int i=1; i<=n; ++i) R[i] = (a*R[i-1]+c)%256;
return R;
}
vector<int> make_O(vector<int> &I,vector<int> &R) {
vector<int> O(n);
for(int i=0; i<n; ++i) O[i] = (I[i]+R[i+1])%256;
return O;
}
double entropy(vector<int> &o) {
int cnt[256];
memset(cnt, 0, sizeof(cnt));
for(int i=0; i<n; ++i) cnt[o[i]]++;
double ret = 0;
for(int i=0; i<256; ++i) {
double t = (double)cnt[i]/(double)n;
if(cnt[i] > 0) ret += -t*log(t);
}
return ret;
}
int main() {
while(cin>>n, n) {
vector<int> I(n);
for(int i=0; i<n; ++i) cin>>I[i];
double mH = 1e300,td;
int aa=0,as=0,ac=0;
for(int s=0; s<=15; ++s) {
for(int a=0; a<=15; ++a) {
for(int c=0; c<=15; ++c) {
vector<int> R = make_R(s,a,c);
vector<int> O = make_O(I,R);
td = entropy(O);
if(td + 1e-10 < mH) {
mH = td;
aa = a,as = s,ac = c;
}else if(abs(td-mH) < 1e-10) {
if(s < as) {
aa = a,as = s,ac = c;
}else if(s == as) {
if(a < aa)
aa = a,as = s,ac = c;
else if(a == aa && c < ac)
aa = a,as = s,ac = c;
}
}
}
}
}
cout<<as<<" "<<aa<<" "<<ac<<endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>
#include <sstream>
#include <map>
#include <queue>
#include <stack>
using namespace std;
const double eps = 1e-8;
int s,a,c;
double getH(vector<int> &input){
double H=0;
const int n=input.size();
vector<int> R;
R.push_back(s);
for(int i=1;i<n;i++) R.push_back((a*R[i-1]+c) % 256);
vector<int> occ(256,0);
for(int i=1;i<R.size();i++) occ[(R[i]+input[i])%256]++;
for(int i=0;i<256;i++){
if(occ[i]!=0) H-=(occ[i]/(double)n)*log((occ[i]/(double)n));
}
return H;
}
int main()
{
int n;
while(cin>>n,n){
vector<int> input(n+1);
input[0]=-1;
for(int i=1;i<=n;i++) cin>>input[i];
double H=1<<20;
int res[3];
for(s=0;s<=15;s++){
for(a=0;a<=15;a++){
for(c=0;c<=15;c++){
double tmp=getH(input);
// printf("s=%d a=%d c=%d -> %f\n",s,a,c,tmp);
if(H-tmp>eps){H=tmp;res[0]=s;res[1]=a;res[2]=c;}
}
}
}
printf("%d %d %d\n",res[0],res[1],res[2]);
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
static const double INF = 1E20;
const int M = 256;
int res[3];
int N;
int I[333];
int R[333], O[333];
double c[333];
int main(int argc, char *argv[])
{
while(cin >> N, N){
rep(i, N) cin >> I[i];
double H = INF;
rep(S, 16) rep(A, 16) rep(C, 16){
R[0] = S;
rep(i, N) R[i + 1] = (A * R[i] + C) % M;
rep(i, N) O[i] = (I[i] + R[i + 1]) % M;
rep(i, M) c[i] = 0.0;
rep(i, N) c[O[i]] += 1.0;
double e = 0.0;
rep(i, M) if(c[i]) e -= c[i] * log2(c[i] / N) / N;
if(H > e){
H = e;
res[0] = S;
res[1] = A;
res[2] = C;
}
}
cout << res[0] << ' ' << res[1] << ' ' << res[2] << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define m 256
const double eps=1e-8;
int main(){
while(1){
int n, input[m+1];
scanf(" %d", &n);
if(n==0) break;
for(int i=1; i<=n; ++i) scanf(" %d", &input[i]);
int as=-1, aa=-1, ac=-1;
double minh=10000000;
for(int s=0; s<=15; ++s){
for(int a=0; a<=15; ++a){
for(int c=0; c<=15; ++c){
int r[m+1], o[m+1];
int occur[m+1];
memset(occur, 0, sizeof(occur));
r[0]=s;
for(int i=1; i<=n; ++i){
r[i]=(a*r[i-1]+c)%m;
o[i]=(input[i]+r[i])%m;
occur[o[i]]++;
//printf("o[%d]=%d\n", i, o[i]);
}
double h=0;
for(int i=0; i<m; ++i){
if(occur[i]==0) continue;
else{
double tmp=(double)occur[i]/n;
//printf("tmp=%lf", tmp);
h-= tmp*log(tmp);
}
}
//printf("s=%d, a=%d, c=%d, h=%lf\n", s, a, c, h);
if(h+eps<minh){
minh=h;
as=s;
aa=a;
ac=c;
}
}
}
}
printf("%d %d %d\n", as, aa, ac);
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2165
#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9+7)
#define endl "\n"
#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")
#define MAX 256
#define NUM 15
#define EPS (1e-10)
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
cout<<fixed<<setprecision(10);
while(true){
int N;
vector<int> I;
double _min = INF;
pair<pair<int,int>,int> ans;
cin>>N;
if(!N) break;
I.resize(N);
for(int i = 0; i < N; i++){
cin>>I[i];
}
for(int i = 0; i <= NUM; i++){
for(int j = 0; j <= NUM; j++){
for(int k = 0; k <= NUM; k++){
vector<int> con(MAX,0);
int R = i;
double sum = 0;
for(int l = 0; l < N; l++){
R = (j*R + k)%MAX;
con[(R+I[l])%MAX]++;
}
for(int l = 0; l < MAX; l++){
if(con[l]){
sum -= ((double)con[l]/N) * log((double)con[l]/N);
}
}
if(sum < _min - EPS){
_min = sum;
ans.first.first = i;
ans.first.second = j;
ans.second = k;
}
}
}
}
cout<<ans.first.first<<" "<<ans.first.second<<" "<<ans.second<<endl;
}
return 0;
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import math
M = 256
def entropy_if_smallest(ctr,bound):
ret = 0
for v in ctr:
if v == 0: continue
ret -= (v / N) * math.log2(v / N)
if ret >= bound:
return None
return ret
def solve(src):
ans_h = float('inf')
ans = None
for s in range(16):
for a in range(16):
for c in range(16):
ctr = [0] * M
r = s
for i in src:
r = (a*r + c) % M
o = (i + r) % M
ctr[o] += 1
h = entropy_if_smallest(ctr,ans_h)
if h is not None:
ans = (s,a,c)
ans_h = h
if ans_h == 0:
return ans
return ans
while True:
N = int(input())
if N == 0: break
src = list(map(int,input().split()))
print(' '.join(map(str,solve(src)))) | PYTHON3 |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import java.util.Scanner;
public class Main
{
public void solve()
{
Scanner cin = new Scanner(System.in);
while(true)
{
int M = 256;
int N = cin.nextInt();
if(N == 0)break;
int[] L = new int[N];
for(int i = 0;i < N;i++)
{
L[i] = cin.nextInt();
}
int S = 0,A = 0,C = 0;
double minH = Double.MAX_VALUE;
for(int s = 0;s <= 15;s++)
{
for(int a = 0;a <= 15;a++)
{
for(int c = 0;c<=15;c++)
{
int[] R = new int[N];
int[] O = new int[256];
for(int i = 0;i < N;i++)
{
if(i == 0)
{
R[i] = (a * s + c) % M;
}else
{
R[i] = (a * R[i - 1] + c)%M;
}
}
for(int i = 0;i < N;i++)
{
O[(L[i] + R[i]) % M]++;
}
double h = 0.0;
for(int i = 0;i < 256;i++)
{
if(O[i] == 0)continue;
h -= (double)O[i] / N * Math.log((double)O[i] / N);
}
if(h + 1e-10 < minH)
{
minH = h;
S = s;
A = a;
C = c;
}
}
}
}
System.out.println(S + " " + A + " " + C);
}
}
public static void main(String[] args)
{
new Main().solve();
}
} | JAVA |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <cmath>
#include <iostream>
using namespace std;
int n, a[256];
int main() {
while (cin >> n, n) {
for (int i = 0; i < n; i++) cin >> a[i];
int mi, mj, mk; long double v = 1e+300L;
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
for (int k = 0; k < 16; k++) {
int b[257] = { 0 }, c[257] = { 0 }; b[0] = i;
for (int l = 1; l <= n; l++) b[l] = (j * b[l - 1] + k) % 256;
for (int l = 0; l < n; l++) c[(a[l] + b[l + 1]) % 256]++;
long double res = 0;
for (int l = 0; l < 256; l++) {
if (c[l]) res -= c[l] * log(1.0L * c[l] / n);
}
if (v - 1e-13L > res) v = res, mi = i, mj = j, mk = k;
}
}
}
cout << mi << ' ' << mj << ' ' << mk << endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 |
import java.util.*;
import java.io.*;
public class Main{
static final Reader sc = new Reader();
static final PrintWriter out = new PrintWriter(System.out,false);
public static void main(String[] args) throws Exception {
while(true){
int n = sc.nextInt();
if(n==0){
break;
}
double ex = 0.0000000001;
int s = 0;
int a = 0;
int c = 0;
double ans = 1000000.0;
int m = 256;
int[] l = new int[n];
for(int i=0;i<n;i++){
l[i] = sc.nextInt();
}
for(int i=0;i<=15;i++){
for(int j=0;j<=15;j++){
for(int k=0;k<=15;k++){
int[] count = new int[256];
int r = i;
for(int d=0;d<n;d++){
r = (j*r+k)%m;
int o = (l[d]+r)%m;
count[o]++;
}
double h = 0.0;
for(int d=0;d<256;d++){
if(count[d]!=0){
h -= (double)count[d]/(double)n*Math.log((double)count[d]/(double)n);
}
}
if(h+ex<ans){
ans = h;
s = i;
a = j;
c = k;
}
}
}
}
out.println(s+" "+a+" "+c);
out.flush();
}
sc.close();
out.close();
}
static void trace(Object... o) { System.out.println(Arrays.deepToString(o));}
}
class Reader {
private final InputStream in;
private final byte[] buf = new byte[1024];
private int ptr = 0;
private int buflen = 0;
public Reader() { this(System.in);}
public Reader(InputStream source) { this.in = source;}
private boolean hasNextByte() {
if (ptr < buflen) return true;
ptr = 0;
try{
buflen = in.read(buf);
}catch (IOException e) {e.printStackTrace();}
if (buflen <= 0) return false;
return true;
}
private int readByte() { if (hasNextByte()) return buf[ptr++]; else return -1;}
private boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
private void skip() { while(hasNextByte() && !isPrintableChar(buf[ptr])) ptr++;}
public boolean hasNext() {skip(); return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
boolean minus = false;
long num = readByte();
if(num == '-'){
num = 0;
minus = true;
}else if (num < '0' || '9' < num){
throw new NumberFormatException();
}else{
num -= '0';
}
while(true){
int b = readByte();
if('0' <= b && b <= '9')
num = num * 10 + (b - '0');
else if(b == -1 || !isPrintableChar(b))
return minus ? -num : num;
else
throw new NoSuchElementException();
}
}
public int nextInt() {
long num = nextLong();
if (num < Integer.MIN_VALUE || Integer.MAX_VALUE < num)
throw new NumberFormatException();
return (int)num;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public char nextChar() {
if (!hasNext()) throw new NoSuchElementException();
return (char)readByte();
}
public String nextLine() {
while (hasNextByte() && (buf[ptr] == '\n' || buf[ptr] == '\r')) ptr++;
if (!hasNextByte()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (b != '\n' && b != '\r') {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int[] nextIntArray(int n) {
int[] res = new int[n];
for (int i=0; i<n; i++) res[i] = nextInt();
return res;
}
public char[] nextCharArray(int n) {
char[] res = new char[n];
for (int i=0; i<n; i++) res[i] = nextChar();
return res;
}
public void close() {try{ in.close();}catch(IOException e){ e.printStackTrace();}};
} | JAVA |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
rr = []
while True:
n = I()
if n == 0:
break
ii = LI()
m = inf
r = '-1'
for s in range(16):
for a in range(16):
for c in range(16):
t = s
f = [0] * 256
for i in ii:
t = (a * t + c) % 256
f[(t + i) % 256] += 1
h = 0
for i in range(256):
if f[i] == 0:
continue
h -= f[i] / n * math.log(f[i] / n)
if m > h + eps:
m = h
r = '{} {} {}'.format(s, a, c)
rr.append(r)
return '\n'.join(map(str, rr))
print(main())
| PYTHON3 |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int M = 256;
int n, I[M];
long double calc(int S, int A, int C){
int r = S, cnt[M];
fill(cnt, cnt+M, 0);
for(int i=0;i<n;i++){
r = (A * r + C) % M;
cnt[(I[i] + r) % M]++;
}
long double res = 0.0;
for(int i=0;i<M;i++) if(cnt[i]) res -= (long double)cnt[i] * (log((double)cnt[i]) - log((double)n));
return res;
}
int main(){
while(cin >> n && n){
for(int i=0;i<n;i++) cin >> I[i];
int ans[3];
long double valm = 1e77;
for(int S=0;S<=15;S++){
for(int A=0;A<=15;A++){
for(int C=0;C<=15;C++){
long double tmp = calc(S, A, C);
if(valm > tmp){
ans[0] = S;
ans[1] = A;
ans[2] = C;
valm = tmp;
}
}
}
}
cout << ans[0] << ' ' << ans[1] << ' ' << ans[2] << endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <math.h>
#include <vector>
using namespace std;
#define rep(i, e) for( int i = 0; i < e; i++ )
#define eps 1e-10
typedef vector<int> vec;
int main(){
int N, M = 256;
while( cin >> N, N){
vec I(N);
rep(i, N) cin >> I[i];
int S, A, C;
double H = 1000000;
rep(s, 16){
rep(a, 16){
rep(c, 16){
int R = s, O, x[256] = {0};
//O[0] = ( I[0] + R[0] ) % M;
double h = 0;//-( O[0] / N ) * log(O[0] / N);
rep(i, N){
R = (a*R+c) % M;
O = (I[i] + R) % M;
x[O]++;
}
rep(i, M){
if( !x[i] ) continue;
double t = (double)x[i] / N;
h -= (t * log(t));
}
if( h+eps < H ){
H = h;
S = s;
A = a;
C = c;
//cout << H << ' ' << s << ' ' << a << ' ' << c << endl;
}
}
}
}
cout << S << ' ' << A << ' ' << C << endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
const double EPS = 1e-9;
const int MOD = 256;
int I[257];
int R[257];
int O[257];
int main() {
int N;
while (cin >> N && N) {
for (int i = 1; i <= N; i++) {
cin >> I[i];
}
int ansS, ansA, ansC;
double ansH = 100000000.0;
for (int S = 0; S <= 15; S++) {
for (int A = 0; A <= 15; A++) {
for (int C = 0; C <= 15; C++) {
double H = 0;
R[0] = S;
memset(O, 0, sizeof(O));
for (int i = 1; i <= N; i++) {
R[i] = (A * R[i - 1] + C) % MOD;
O[(I[i] + R[i]) % MOD]++;
}
for (int i = 0; i < 256; i++) {
if (O[i] != 0) H -= (O[i] / (double)(N)* log(O[i] / (double)(N)));
}
if (H + EPS < ansH) {
ansH = H;
ansS = S;
ansA = A;
ansC = C;
}
}
}
}
cout << ansS << ' ' << ansA << ' ' << ansC << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>
using namespace std;
#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
int main(void) {
int i,j;
int n;
int m = 256;
while(cin >> n, n){
vi in(n);
rep(i,n) cin >> in[i];
double H = INF, S, A, C;
rep(s,16)rep(a,16)rep(c,16){
int cnt[256] = {};
int r = s;
rep(i,n){
r = (a * r + c) % m;
cnt[(in[i]+r)%m]++;
}
double out = 0;
rep(i,256)if(cnt[i]){
double t = 1.*cnt[i]/n;
out -= t*log2(t);
}
if(out < H){
H = out;
S = s;
A = a;
C = c;
}
}
cout << S << " " << A << " " << C << endl;
}
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(__typeof((X).begin()) it=(X).begin();it!=(X).end();it++)
#define ite iterator
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())
using namespace std;
struct timer{
time_t start;
timer(){start=clock();}
~timer(){cerr<<1.*(clock()-start)/CLOCKS_PER_SEC<<" secs"<<endl;}
};
typedef istringstream iss;
typedef long long ll;
typedef pair<int,int> pi;
typedef stringstream sst;
typedef vector<int> vi;
int N,I[266],R[266],O[266],M=256;
int S,A,C;
double H;
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
while(cin>>N && N){
rep2(i,1,N+1)cin>>I[i];
H=INF;
rep(s,16)rep(a,16)rep(c,16){
R[0]=s;
rep2(i,1,N+1)R[i]=(a*R[i-1]+c)%M;
rep2(i,1,N+1)O[i]=(I[i]+R[i])%M;
double res=0;
double x[266]={};
rep2(i,1,N+1)x[O[i]]++;
rep(i,M)if(x[i])res-=x[i]/N*log2(x[i]/N);
if(res<H-EPS){
H=res;
S=s,A=a,C=c;
}
}
cout<<S<<" "<<A<<" "<<C<<endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
constexpr double EPS = 1e-6;
constexpr int M = 256;
int S, A, C;
int R() {
return S = (A * S + C) % M;
}
int main() {
int N;
while(cin >> N, N) {
double H = 1e9;
int ret_s=1e9, ret_a=1e9, ret_c=1e9;
vector<int> v(N);
for(int i=0; i<N; ++i) {
cin >> v[i];
}
for(int s=0; s<16; ++s) {
for(int a=0; a<16; ++a) {
for(int c=0; c<16; ++c) {
S = s, A = a, C = c;
double h = 0;
map<int, int> m;
for(int n=0; n<N; ++n) {
int o = (v[n] + R()) % M;
m[o] += 1;
}
for(auto x : m) {
h -= static_cast<double>(x.second) / N * log(static_cast<double>(x.second)/N);
}
if(h + EPS < H) {
H = h;
ret_s = s;
ret_a = a;
ret_c = c;
}
}
}
}
cout << ret_s << " " << ret_a << " " << ret_c << endl;
}
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
const double INF = (double)(1 << 28);
const int MOD = 256;
const double EPS = 1e-10;
int main () {
int n;
while (cin >> n, n) {
vector<int> I(n);
for (auto &i : I) cin >> i;
double H = INF;
int S, A, C;
for (int s = 0; s < 16; s++) {
for (int a = 0; a < 16; a++) {
for (int c = 0; c < 16; c++) {
vector<int> o(MOD, 0);
int r = s;
for (int i = 0; i < n; i++) {
r = (r * a + c) % MOD;
o[(r + I[i]) % MOD]++;
}
double h = 0.;
for (auto i : o) {
if (i > 0) h -= ((double)i / n) * log((double)i / n);
}
if (h + EPS < H) {
H = h, S = s, A = a, C = c;
}
}
}
}
cout << S << " " << A << " " << C << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<iostream>
#include<cmath>
using namespace std;
#define M 256
int I[M+1];
int R[M+1];
int O[M+1];
int N;
long double getEntropy( int S, int A, int C ){
R[0] = S;
for ( int i = 1; i <= N; i++ ){
R[i] = (A*R[i-1]+C)%M;
}
for ( int i = 1; i <= N; i++ ){
O[i] = (I[i] + R[i])%M;
}
long double H = 0;
int cnt[M+1];
for ( int i = 0; i <= M; i++ ) cnt[i] = 0;
for ( int i = 1; i <= N; i++ ) cnt[O[i]]++;
for ( int i = 0; i <= M; i++ ){
if ( cnt[i] > 0){
long double x = 1.0*cnt[i]/N;
H += x*log(x);
}
}
return -1*H;
}
#define EPS (1e-12)
void compute(){
int S, A, C;
long double minv = (1<<21);
//long double maxv = -1*(1<<21);
for ( int s = 0; s <= 15; s++ ){
for ( int a = 0; a <= 15; a++ ){
for ( int c = 0; c <= 15; c++ ){
long double e = getEntropy(s, a, c);
if ( minv-EPS > e ){
// cout << e << endl;
S = s; A = a; C = c;
minv = e;
}
}
}
}
cout << S << " " << A << " " << C << endl;
}
int main(){
while(1){
cin >> N;
if ( N == 0 ) break;
for ( int i = 1; i <= N; i++ ) cin >> I[i];
compute();
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"
using namespace std;
constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;
//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> N, N) {
vector<int>v(N);
long double score = MOD;
int a = 0, b = 0, c = 0;
for (auto &i : v)cin >> i;
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
for (int k = 0; k < 16; k++) {
int p = i;
vector<int>w(256);
for (int l = 0; l < N; l++) {
p = p * j + k;
p %= 256;
w[(v[l] + p) % 256]++;
}
long double cscore = 0;
for (int l = 0; l < 256; l++) {
if(w[l])cscore -= logl(w[l])*w[l];
}
//cout << i << " " << j << " " << k << " " << cscore << endl;
if (cscore +EPS< score) {
score = cscore;
a = i, b = j, c = k;
}
}
}
}
cout << a << " " << b << " " << c << endl;
}
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<cstring>
#include<cctype>
using namespace std;
#define M 256
const double infty = 1e40;
bool eq(double a, double b){
return abs(b-a) < 1e-9;
}
int main()
{
while(true){
int n;
cin >> n;
if( n == 0 ) break;
vector<int> I;
for(int i = 0; i < n; ++i){
int t;
cin >> t;
I.push_back(t);
}
double minH = infty;
pair<int,pair<int,int> > SAC;
for(int S = 0; S <= 15; ++S){
for(int A = 0; A <= 15; ++A){
for(int C = 0; C <= 15; ++C){
int cnt[M];
vector<int> O;
vector<int> R;
for(int i = 0; i < M; ++i){
cnt[i] = 0;
}
for(int i = 0; i < n; ++i){
R.push_back( (A* (i-1<0?S:R[i-1])+C) % M );
O.push_back( (I[i]+R[i]) % M );
cnt[O[i]]++;
}
bool ng = false;
for(int i = 0; i < (int)O.size(); ++i){
if(!isupper(O[i])&&!islower(O[i]))ng=true;
}
//if(ng)continue;
double H = 0;
for(int i = 0; i < M; ++i){
if( cnt[i] > 0 ){
double tl = (cnt[i]/(double)n) * log(cnt[i]/(double)n);
H += tl;
}
}
H = -H;
/*
cout << S << ' ' << A << ' ' << C << ' ' << occ << ' ' << len << " : " << H << endl;
for(int i = 0; i < (int)O.size(); ++i){
cout << O[i] << ' ';
}
cout << endl;
*/
if( eq(minH, H) ){
SAC = min(SAC,make_pair(S,make_pair(A,C)));
//cout << S << ' ' << A << ' ' << C << " : " << H << endl;
}else if( minH > H ){
minH = H;
SAC = make_pair(S,make_pair(A,C));
//cout << S << ' ' << A << ' ' << C << " : " << H << endl;
}
}
}
}
cout << SAC.first << ' ' << SAC.second.first << ' ' << SAC.second.second << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Arrays;
public class Main {
static FastScanner sc = new FastScanner();
public static void main(String[] args) throws Exception {
int[] hist = new int[256];
while (true) {
int N = sc.nextInt();
if (N == 0) break;
int[] I = new int[N];
for (int i = 0; i < N; ++i) {
I[i] = sc.nextInt();
}
double best = Double.MAX_VALUE;
int bs = 0;
int ba = 0;
int bc = 0;
for (int s = 0; s < 16; ++s) {
for (int a = 0; a < 16; ++a) {
for (int c = 0; c < 16; ++c) {
Arrays.fill(hist, 0);
int r = s;
for (int i = 0; i < N; ++i) {
r = (a * r + c) & 0xFF;
hist[(I[i] + r) & 0xFF]++;
}
double e = 0;
for (int i = 0; i < 256; ++i) {
if (hist[i] != 0) {
double v = 1.0 * hist[i] / N;
e -= v * Math.log(v);
if (e >= best) break;
}
}
if (e < best - 1e-10) {
best = e;
bs = s;
ba = a;
bc = c;
}
}
}
}
System.out.println(bs + " " + ba + " " + bc);
}
}
static class FastScanner {
Reader input;
FastScanner() {
this.input = new BufferedReader(new InputStreamReader(System.in));
}
int nextInt() throws IOException {
int sign = 1;
int b = input.read();
while ((b < '0' || '9' < b) && b != '-' && b != '+') {
b = input.read();
}
if (b == '-') {
sign = -1;
b = input.read();
} else if (b == '+') {
b = input.read();
}
int ret = b - '0';
while (true) {
b = input.read();
if (b < '0' || '9' < b) return ret * sign;
ret *= 10;
ret += b - '0';
}
}
}
} | JAVA |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE
#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
int n, m;
double calc(vector<int> &cnt){
double res = 0;
for (double x : cnt){
if(x) res -= x / n*log(x / n);
}
return res;
}
tuple<int, int, int> bf(vector<int> &v){
double opt = 1e9;
auto ans = MT(-1, -1, -1);
rep(s, 16) rep(a, 16) rep(c, 16){
vector<int> cnt(256);
int r = s;
rep(i, v.size()){
r = (a*r + c) % m;
++cnt[(v[i] + r) % m];
}
double tmp = calc(cnt);
if (tmp + 1e-9 < opt){
ans = MT(s, a, c);
opt = tmp;
}
}
return ans;
}
int main(){
m = 256;
while (cin >> n, n){
vector<int> v(n);
rep(i, n) cin >> v[i];
int s, a, c;
tie(s, a, c) = bf(v);
cout << s << ' ' << a << ' ' << c << '\n';
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import java.util.*;
import java.math.*;
public class Main{
private static int S,A,C;
private static int[] I, Rmemo;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(true){
int n = sc.nextInt();
if(n == 0) break;
I = new int[n];
for(int i=0;i<n;i++) I[i] = sc.nextInt();
double minH = Double.MAX_VALUE;
int minS=-1, minA=-1, minC=-1;
for(S=0;S<=15;S++){
for(A=0;A<=15;A++){
for(C=0;C<=15;C++){
double H = 0.0;
Rmemo = new int[258];
Arrays.fill(Rmemo,-1);
int[] count = new int[258];
for(int i=1;i<=n;i++) count[O(i)]++;
for(int x : count){
if(x > 0)
H -= (double)x / n * Math.log((double)x / n);
}
if(minH > H && Math.abs(minH-H) > 0.00001){
minH = H;
minS = S;
minA = A;
minC = C;
}
}
}
}
System.out.println(minS + " " + minA + " " + minC);
}
}
private static int O(int i){
return (I[i-1] + R(i)) % 256;
}
private static int R(int i){
if(i == 0) return S;
if(Rmemo[i] != -1) return Rmemo[i];
return Rmemo[i] = (A * R(i-1) + C) % 256;
}
} | JAVA |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!
#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
const int M = 256;
int N;
while (cin >> N, N) {
V I(N);
rep(i, N) cin >> I[i];
double mi = 1e9;
int S, A, C;
rep(s, 15 + 1) {
rep(a, 15 + 1) {
rep(c, 15 + 1) {
int r = s;
map<int, int> mp; // O(i), #occurence
rep(i, N) {
r = (a * r + c) % M;
mp[(I[i] + r) % M]++;
}
double h = 0.;
for (auto& p : mp) {
h += - p.second * log((p.second * 1.) / N);
}
if (h + 1e-10 < mi) {
mi = h;
S = s, A = a, C = c;
}
}
}
}
cout << S << " " << A << " " << C << endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
const int INF = 100000000;
using namespace std;
int main(){
int n;
while(cin >> n, n){
int s, a, c, m = 256, inp[256];
double ans[4];
ans[0] = INF;
rep(i,n) cin >> inp[i];
for(s = 0; s <= 15; s++){
for(a = 0; a <= 15; a++){
for(c = 0; c <= 15; c++){
int r[300];
double ental = 0;
int cnt[256] = {0};
rep(i,300){
if(i == 0) r[i] = s;
else r[i] = (a * r[i - 1] + c) % m;
}
rep(i,n){
cnt[(inp[i] + r[i + 1]) % m]++;
}
rep(i,256){
double temp = static_cast<double>(cnt[i]);
double N = static_cast<double>(n);
if(cnt[i] == 0) continue;
ental += -1 * log(temp / N) * temp / N;
}
if(ans[0] > ental + 1e-9){
ans[0] = ental;
ans[1] = s;
ans[2] = a;
ans[3] = c;
}
}
}
}
range(i,1,4){
if(i != 1) cout << ' ';
cout << ans[i];
}
cout << endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define mp(a,b) make_pair(a,b)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
#define INF INT_MAX/3
#define MAX_N 1000
const int M=256;
int n;
int l[333];
int r[333];
double f[333];
int main(){
cin.sync_with_stdio(false);
while(1){
cin>>n;
if(n==0)break;
rep(i,n)cin>>l[i];
double minh=INF;
int ress=0,resa=0,resc=0;
rep(s,16)rep(a,16)rep(c,16){
r[0]=s;
repl(i,1,n+1){
r[i]=(a*r[i-1]+c)%M;
}
memset(f,0,sizeof(f));
repl(i,1,n+1)f[(l[i-1]+r[i])%M]++;
double h=0;
rep(i,M){
if(f[i]!=0)h-=(f[i]/n)*log(f[i]/n);
}
if(h+1e-8<minh){
minh=h;
ress=s;
resa=a;
resc=c;
}
}
printf("%d %d %d\n",ress,resa,resc);
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
int n;
while(cin>>n,n){
int a[n],a1,a2,a3;
double mi=0;
r(i,n)cin>>a[i];
r(i,16)r(j,16)r(k,16){
int R=i,b[256]={};
r(l,n){
R=(j*R+k)%256;
b[(a[l]+R)%256]++;
}
double h=0;
r(l,256)if(b[l])
h-=(double)b[l]*log((double)b[l]);
if(mi>h){
mi=h;
a1=i;
a2=j;
a3=k;
}
}
cout<<a1<<' '<<a2<<' '<<a3<<endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
int n;
int T[1024];
int E[1024];
int main() {
for(;;) {
scanf("%d", &n);
if( n == 0 ) break;
for(int i = 0; i < n; ++i) {
scanf("%d", &E[i]);
}
double bH = 1e10;
int bS, bA, bC;
for(int S = 0; S < 16; ++S) {
for(int A = 0; A < 16; ++A) {
for(int C = 0; C < 16; ++C) {
for(int i = 0; i < 256; ++i) T[i] = 0;
int R = S;
double H = 0.0;
for(int k = 0; k < n; ++k) {
R = (A * R + C) % 256;
int t = (E[k] + R) % 256;
T[t] += 1;
}
for(int k = 0; k < 256; ++k) {
if( T[k] == 0 ) continue;
double t = (double)T[k] / n;
H -= t * log(t);
}
if( bH - H > 0.00000000001 ) {
bH = H;
bS = S;
bA = A;
bC = C;
}
}
}
}
printf("%d %d %d\n", bS, bA, bC);
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <math.h>
using namespace std;
typedef long long ll;
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int mod=256;
while(1){
int n; cin >> n;
if(n==0)break;
vector<int> x(n);
for(int i=0;i<n;i++){
cin >> x[i];
}
double h=1e9;
int s,a,c;
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
for(int k=0;k<16;k++){
int cnt[256]={};
int r=i;
for(int l=0;l<n;l++){
r=(j*r+k)%mod;
cnt[(x[l]+r)%mod]++;
}
double cul=0;
for(int l=0;l<256;l++){
if(cnt[l]){
double t=1.0*cnt[l]/n;
cul-=t*log2(t);
}
}
if(cul<h){
h=cul;
s=i,a=j,c=k;
}
}
}
}
cout << s << " " << a << " " << c << endl;
}
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<cstdio>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
int n,l[256],x,y,z;
while(scanf("%d",&n),n){
rep(i,n)scanf("%d",&l[i]);
double m = 1e9;
rep(s,16)rep(a,16)rep(c,16){
int u[256] = {0};
int r = s;
rep(i,n){
r = (a*r + c)&255;
u[(r + l[i])&255]++;
}
double tmp = 0;
rep(i,256)if(u[i]){
tmp -= u[i]*log((double)u[i]/n);
if(m + 1e-9 < tmp)break;
}
if(m > tmp + 1e-9){ m = tmp; x = s; y = a; z = c; }
}
printf("%d %d %d\n",x,y,z);
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import java.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;
public class Main {
static final Scanner in = new Scanner(System.in);
static final PrintWriter out = new PrintWriter(System.out,false);
static boolean debug = false;
static boolean solve() {
int n = in.nextInt();
if (n == 0) return false;
int[] a = new int[n+1];
for (int i=0; i<n; i++) {
a[i+1] = in.nextInt();
}
int[] r = new int[257];
int[] o = new int[257];
int ai = -1, aj = -1, ak = -1;
double h = 1<<10;
for (int i=0; i<=15; i++) {
for (int j=0; j<=15; j++) {
for (int k=0; k<=15; k++) {
double tmp = 0;
Arrays.fill(o, 0);
r[0] = i;
for (int l=1; l<=n; l++) {
r[l] = (j*r[l-1]+k) % 256;
o[(a[l] + r[l]) % 256] += 1;
}
for (int l=0; l<256; l++) {
if (o[l] != 0)
tmp -= (double)o[l] / n * Math.log((double)o[l] / n);
}
if (tmp + 1e-9 < h) {
h = tmp;
ai = i;
aj = j;
ak = k;
}
}
}
}
out.println(ai+" "+aj+" "+ak);
return true;
}
public static void main(String[] args) {
debug = args.length > 0;
long start = System.nanoTime();
while(solve());
out.flush();
long end = System.nanoTime();
dump((end - start) / 1000000 + " ms");
in.close();
out.close();
}
static void dump(Object... o) { if (debug) System.err.println(Arrays.deepToString(o)); }
} | JAVA |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 |
import java.util.*;
import static java.lang.System.*;
class Main {
public static Scanner sc = new Scanner(in);
public static Random rand=new Random();
int m=256;
double EPS=1E-6;
public void run() {
while(true){
int N=sc.nextInt();
if(N==0)return;
int[] l=nextIntArray(N);
double resh=Double.MAX_VALUE;
int ress=-1,resa=-1,resc=-1;
for(int s=0;s<=15;s++)
for(int a=0;a<=15;a++)
for(int c=0;c<=15;c++){
int[] r=new int[N+1];
int[] o=new int[m];
r[0]=s;
for(int i=1;i<=N;i++){
r[i]=(a*r[i-1]+c)%m;
o[(r[i]+l[i-1])%m]++;
}
double h=0;
for(int i=0;i<m;i++){
if(o[i]!=0)h-=((double)o[i]/N)*Math.log((double)o[i]/N);
}
if(h+EPS<resh){
resh=h;
ress=s;
resa=a;
resc=c;
}
}
ln(ress+" "+resa+" "+resc);
}
}
public static void main(String[] args) {
new Main().run();
}
public int[] nextIntArray(int n){
int[] res=new int[n];
for(int i=0;i<n;i++){
res[i]=sc.nextInt();
}
return res;
}
public static void pr(Object o) {
out.print(o);
}
public static void ln(Object o) {
out.println(o);
}
public static void ln() {
out.println();
}
} | JAVA |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<bits/stdc++.h>
#define N 257
#define M 256
using namespace std;
typedef pair<double,int> P0;
typedef pair<int,int> P1;
typedef pair<P0,P1> P;
int main(){
int n,l[N],R[N],O[N],cnt[N];
double H;
vector<P> v;
while(1){
cin>>n;
if(!n)break;
for(int i=1;i<=n;i++)cin>>l[i];
for(int S=0;S<16;S++){
R[0]=S;
for(int A=0;A<16;A++)
for(int C=0;C<16;C++){
for(int i=0;i<N;i++)cnt[i]=0;
for(int i=1;i<=n;i++){
R[i]=(A*R[i-1]+C)%M;
O[i]=(l[i]+R[i])%M;
cnt[O[i]]++;
}
H=0;
for(int i=0;i<N;i++){
if(!cnt[i])continue;
H-=(1.0*cnt[i]/n)*log(1.0*cnt[i]/n);
}
H=((int)(H*1000000))/1000000.0;
v.push_back(P(P0(H,S),P1(A,C)));
}
}
sort(v.begin(),v.end());
cout<<v[0].first.second<<' ';
cout<<v[0].second.first<<' ';
cout<<v[0].second.second<<endl;
v.clear();
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
inline bool chmin(double &a, double b){
if (a - 1e-8 > b){
a = b;
return true;
}
return false;
}
const int M = 256;
int N;
int I[257], R[257], O[257];
int cnt[257];
double calc(int s, int a, int c)
{
R[0] = s;
for (int i = 1; i <= N; i++){
R[i] = (a * R[i - 1] + c) % M;
}
memset(cnt, 0, sizeof cnt);
for (int i = 1; i <= N; i++){
O[i] = (I[i] + R[i]) % M;
cnt[O[i]]++;
}
double H = 0;
for (int i = 0; i < M; i++){
if (!cnt[i]) continue;
H -= double(cnt[i]) / N * log(double(cnt[i]) / N);
}
return H;
}
int main()
{
while (scanf("%d", &N), N){
for (int i = 1; i <= N; i++) scanf("%d", I + i);
double mini = 1e9;
int ress, resa, resc;
for (int s = 0; s < 16; s++)
for (int a = 0; a < 16; a++)
for (int c = 0; c < 16; c++)
if (chmin(mini, calc(s, a, c))){
ress = s;
resa = a;
resc = c;
}
printf("%d %d %d\n", ress, resa, resc);
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <vector>
#include <cmath>
using namespace std;
#define EPS 1e-10
int main() {
int n;
while (cin >> n, n) {
vector<int> I(n);
for (auto& a : I) cin >> a;
int s, a, c;
double h = 1e18;
for (int i = 0; i < 16; ++i) {
for (int j = 0; j < 16; ++j) {
for (int k = 0; k < 16; ++k) {
int cnt[256] = {};
int r = i;
for (int h = 0; h < n; ++h) {
r = (j * r + k) % 256;
cnt[(I[h] + r) % 256]++;
}
double tmp = 0;
for (int h = 0; h < 256; ++h) {
if (cnt[h] == 0) continue;
tmp -= (double)cnt[h] / n * log((double)cnt[h] / n);
}
if (tmp + EPS < h) h = tmp, s = i, a = j, c = k;
}
}
}
cout << s << " " << a << " " << c << endl;
}
return 0;
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <cstdio>
#include <vector>
#include <complex>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <stack>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <cassert>
using namespace std;
typedef long long ll;
typedef ll li;
typedef pair<int,int> PI;
#define EPS (1e-10L)
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define F first
#define S second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(),a.end()
#define FOR(it,a) for(__typeof(a.begin())it=a.begin();it!=a.end();++it)
void pkuassert(bool t){t=1/t;};
int dx[]={0,1,0,-1,1,1,-1,-1};
int dy[]={1,0,-1,0,-1,1,1,-1};
int n;
int L[1000];
void solve(){
rep(i,n) cin >> L[i];
pair<pair<double,int>,PI> ans;
ans.F.F=10000000000000LL;
rep(s,16)rep(a,16)rep(c,16){
int r=s;
map<int,int> app;
rep(i,n){
r=(a*r+c)&255;
int o=(r+L[i])&255;
app[o]++;
}
double p=0;
FOR(it,app) p-=it->S*log(it->S*1./n)/n;
if(ans.F.F>p+EPS){
ans=mp(mp(p,s),mp(a,c));
}
}
cout << ans.F.S << ' ' << ans.S.F << ' ' << ans.S.S << endl;
}
int main(int argc, char *argv[])
{
while(cin >> n && n) solve();
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 1145141919
#define MOD 1000000007
#define EPS 1e-6
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge
{
int from, to, cost;
bool operator < (const edge& e) const { return cost < e.cost; }
bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///
int main()
{
int n;
while (cin >> n, n)
{
tuple<double, int, int, int> ans;
vi l(n);
REP(i, n) cin >> l[i];
ans = make_tuple(INF, -1, -1, -1);
REP(s, 16)REP(a, 16)REP(c, 16)
{
vi r(n+1);
r[0] = s;
REP(i, n) r[i + 1] = ((a*r[i]) + c) % 256;
vi o(n);
double cnt[256] = {};
REP(i, n)
{
o[i] = (l[i] + r[i + 1]) % 256;
cnt[o[i]]++;
}
double h = 0;
REP(i, 256)
{
if (cnt[i] > 0) h -= (cnt[i] / n)*log(cnt[i] / n);
}
if (h + EPS < get<0>(ans)) ans = make_tuple(h, s, a, c);
}
cout << get<1>(ans) << " " << get<2>(ans) << " " << get<3>(ans) << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
int INF = 10000;
int main(){
while (1){
int N;
cin >> N;
if (N == 0){
break;
}
vector<int> l(N);
for (int i = 0; i < N; i++){
cin >> l[i];
}
double m = INF;
int s, a, c;
for (int S = 0; S < 16; S++){
for (int A = 0; A < 16; A++){
for (int C = 0; C < 16; C++){
vector<int> R(N + 1);
R[0] = S;
for (int i = 0; i < N; i++){
R[i + 1] = (R[i] * A + C) % 256;
}
vector<int> O(N);
for (int i = 0; i < N; i++){
O[i] = (l[i] + R[i + 1]) % 256;
}
vector<int> cnt(256, 0);
for (int i = 0; i < N; i++){
cnt[O[i]]++;
}
double tmp = 0;
for (int i = 0; i < 256; i++){
if (cnt[i] > 0){
double r = (double) cnt[i] / N;
tmp -= r * log(r);
}
}
if (tmp < m - 0.001){
m = tmp;
s = S;
a = A;
c = C;
}
}
}
}
cout << s << ' ' << a << ' ' << c << endl;
}
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO θͺεηζγγγγ‘γ½γγγ»γΉγΏγ
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
int n = Integer.parseInt(br.readLine());
if(n == 0){
break;
}
int input[] = new int[n];
String[] tmpArray = br.readLine().split(" ");
for(int i = 0; i < n; i++){
input[i] = Integer.parseInt(tmpArray[i]);
}
int ansS = 0, ansA = 0, ansC = 0;
double result = Double.MAX_VALUE;
//ε
¨ζ’η΄’
for(int s = 0; s <= 15; s++){
for(int a = 0; a <= 15; a++){
for(int c = 0; c <= 15; c++){
double tmpEntropy = calcEntropy(generateOutput(input, s, a, c));
// if(s == 8){
// System.out.println("s a c"+ s+" "+ a+" " + c + " entropy "+tmpEntropy);
// }
if(tmpEntropy < result){
result = tmpEntropy;
ansS = s; ansA = a; ansC = c;
}
}
}
}
System.out.println(ansS + " "+ ansA +" "+ ansC);
}
}
static int[] generateOutput(int[] input, int s, int a, int c){
int n = input.length;
int[] r = new int[n + 1];
r[0] = s;
for(int i = 1; i <= n; i++){
r[i] = (a*r[i - 1] + c) % 256;
}
int[] out = new int[n];
for(int i = 0; i < n; i++){
out[i] = (input[i] + r[i + 1]) % 256;
}
return out;
}
static double calcEntropy(int[] array){
int[] count = new int[256];
int n = array.length;
for(int i = 0; i < n; i++){
count[array[i]]++;
}
double result = 0;
for(int i = 0; i < count.length; i++){
if(count[i] != 0){
// System.out.println(i+" appears "+count[i]+" times");
result += (double)count[i]/n * Math.log((double)count[i]/n)/Math.log(2);
}
}
//System.out.println("entropy "+(-result));
return -result;
}
}
| JAVA |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Arrays;
public class Main {
static FastScanner sc = new FastScanner();
public static void main(String[] args) throws Exception{
int[] hist = new int[256];
while (true) {
int N = sc.nextInt();
if (N == 0) break;
int[] I = new int[N];
for (int i = 0; i < N; ++i) {
I[i] = sc.nextInt();
}
double best = Double.MAX_VALUE;
int bs = 0;
int ba = 0;
int bc = 0;
for (int s = 0; s < 16; ++s) {
for (int a = 0; a < 16; ++a) {
for (int c = 0; c < 16; ++c) {
Arrays.fill(hist, 0);
int r = s;
for (int i = 0; i < N; ++i) {
r = (a * r + c) & 0xFF;
hist[(I[i] + r) & 0xFF]++;
}
double e = 0;
for (int i = 0; i < 256; ++i) {
if (hist[i] != 0) {
double v = 1.0 * hist[i] / N;
e -= v * Math.log(v);
}
}
if (e < best - 1e-10) {
best = e;
bs = s;
ba = a;
bc = c;
}
}
}
}
System.out.println(bs + " " + ba + " " + bc);
}
}
static class FastScanner {
Reader input;
FastScanner() {
this.input = new BufferedReader(new InputStreamReader(System.in));
}
int nextInt() throws IOException {
int sign = 1;
int b = input.read();
while ((b < '0' || '9' < b) && b != '-' && b != '+') {
b = input.read();
}
if (b == '-') {
sign = -1;
b = input.read();
} else if (b == '+') {
b = input.read();
}
int ret = b - '0';
while (true) {
b = input.read();
if (b < '0' || '9' < b) return ret * sign;
ret *= 10;
ret += b - '0';
}
}
}
} | JAVA |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <vector>
#include <tuple>
#include <numeric>
#include <cmath>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define all(c) (c).begin(), (c).end()
const double eps = 1e-8, inf = 1e8;
const int M = 256;
int N;
tuple<int, int, int> solve(vector<int>& Is){
int S = 0, A = 0, C = 0;
double Hmin = inf;
rep(s, 16)rep(a, 16)rep(c, 16){
vector<int> Os(M, 0);
int R = s;
for(int& I: Is){
R = (a * R + c) % M;
Os[(I + R) % M]++;
}
double H = accumulate(all(Os), 0.0, [&](double h, int x){return x? h - 1.0 * x/N * log(1.0 * x/N): h;});
if(Hmin <= H + eps)continue;
Hmin = H;
S = s; A = a; C = c;
}
return make_tuple(S, A, C);
}
int main(){
while(cin >> N, N){
vector<int> Is(N);
rep(i, N)cin >> Is[i];
int S, A, C;
tie(S, A, C) = solve(Is);
cout << S << " " << A << " " << C << '\n';
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <cmath>
#include <iostream>
using namespace std;
int n, a[261], b[261];
int main() {
while (cin >> n, n) {
for (int i = 0; i < n; i++) cin >> a[i];
int mi, mj, mk; double v = 1e+300;
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
for (int k = 0; k < 16; k++) {
int c[261] = { 0 }; b[0] = i;
for (int l = 1; l <= n; l++) b[l] = (j * b[l - 1] + k) % 256;
for (int l = 0; l < n; l++) c[(a[l] + b[l + 1]) % 256]++;
double res = 0;
for (int l = 0; l < 256; l++) {
if (c[l]) res -= c[l] * log(1.0 * c[l] / n);
}
if (v - 1e-9 > res) v = res, mi = i, mj = j, mk = k;
}
}
}
cout << mi << ' ' << mj << ' ' << mk << endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define pi M_PI
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
int main() {
int n, m = 256;
while (cin >> n && n) {
VI i(n);
REP(j,n) cin >> i[j];
double mi = 1e9;
int sa, aa, ca;
REP(s,16) REP(a,16) REP(c,16) {
VI r(n+1), o(n);
r[0] = s;
REP(j,n) r[j+1] = (a*r[j] + c) % m;
REP(j,n) o[j] = (i[j] + r[j+1]) % m;
vector<double> p(m);
REP(j,n) p[o[j]] += 1.0;
double h = 0.0;
REP(j,m) if(p[j]>0) h -= p[j]/n * log(p[j]/n);
if (h < mi - 1e-10) {
mi = h;
sa = s;
aa = a;
ca = c;
}
}
printf("%d %d %d\n", sa, aa, ca);
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repp(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
typedef long long LL;
typedef long double LD;
int main(){
while(1){
int n;
cin >> n;
if(n==0) return 0;
vector<int> v(n);
rep(i,n) cin >> v[i];
LD h=IINF;
int ss,aa,cc;
rep(s,16){
rep(a,16){
rep(c,16){
LD tmp=0;
vector<LD> count(256,0);
vector<int> r(n+1);
r[0]=s;
repp(i,1,n+1) r[i]=(a*r[i-1]+c)%256;
rep(i,n){
count[(v[i]+r[i+1])%256]++;
}
rep(i,256){
if(count[i]>0)
tmp-=count[i]/n*log(count[i]/n);
}
if(tmp<h-0.00001){
h=tmp;
ss=s,aa=a,cc=c;
}
}
}
}
cout << ss SP aa SP cc << endl;
}
return 0;
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <iomanip>
using namespace std;
#define pi 3.141592653589793
int main()
{
int r[16][16][16][257];
for(int s = 0; s < 16; s++){
for(int a = 0; a < 16; a++){
for(int c = 0; c < 16; c++){
for(int i = 0; i < 257; i++){
if(i == 0) r[s][a][c][i] = s;
else r[s][a][c][i] = (a * r[s][a][c][i - 1] + c) % 256;
}
}
}
}
int n;
int i[257];
while(1){
cin >> n;
if(n == 0) break;
for(int j = 1; j < n + 1; j++){
cin >> i[j];
}
double minh = 1000000, ans_s = 0, ans_a = 0, ans_c = 0;
for(int s = 0; s < 16; s++){
for(int a = 0; a < 16; a++){
for(int c = 0; c < 16; c++){
int cnt[257] = {};
int maxo = 0;
for(int j = 1; j < n + 1; j++){
int o = (i[j] + r[s][a][c][j]) % 256;
cnt[o]++;
maxo = max(maxo, o);
}
double h = 0;
for(int i = 0; i < maxo + 1; i++){
if(cnt[i] != 0){
h -= cnt[i] * log(cnt[i]);
}
}
h = h / n;
h += log(n);
// cout << s << " " << a << " " << c << " " << h << endl;
if(h < minh){
ans_s = s;
ans_a = a;
ans_c = c;
minh = h;
}
}
}
}
cout << ans_s << " " << ans_a << " " << ans_c << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 256
#define EPS 1e-8
using namespace std;
typedef long long ll;
int main()
{
int n;
while(cin >> n && n) {
vector<int> I(n);
rep(i,n) cin >> I[i];
int S = 0,A = 0,C = 0;
double H = 100000.0;
rep(s,16) {
rep(a,16) {
rep(c,16) {
double cnt[500];
memset(cnt,0,sizeof(cnt));
int res = s;
rep(i,n) {
res = (a * res + c) % MOD;
int t = (I[i] + res) % MOD;
cnt[t]++;
}
double ret = 0.0;
rep(i,MOD) {
if(cnt[i]) {
ret -= cnt[i] / n * log(cnt[i] / (double)n);
}
}
// cout << ret << " "<< EPS << " " << ret + EPS << " " << S << endl;
if(ret + EPS < H) {
H = ret;
S = s;
A = a;
C = c;
}
}
}
}
cout << S << " " << A << " " << C << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 |
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>
#include <iomanip>
using namespace std;
#define pb push_back
typedef long long ll;
typedef complex<int> P;
#define EPS 1e-10
double f(int s, int a, int c, vector<int> &l, int n){
int r = s;
int x[256] = {0};
for(int i=0;i<n;i++){
r = (a * r + c) % 256;
int o = (l[i] + r) % 256;
x[o]++;
}
double h = 0;
for(int i=0;i<256;i++){
if(x[i]) h -= (double)x[i]/n * log((double)x[i]/n);
}
return h;
}
bool solve(){
int n;
cin>> n;
if(!n) return false;
vector<int> v(n);
for(int i=0;i<n;i++){
cin>> v[i];
}
double h = 1e20;
int s, a, c;
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
for(int k=0;k<16;k++){
double h2 = f(i, j, k, v, n);
if(h2 + EPS < h){
h = h2;
s = i;
a = j;
c = k;
}
}
}
}
cout<< s<< " "<< a<< " "<< c<< endl;
return true;
}
int main(){
cout.setf(ios::fixed);
cout.precision(10);
while(solve());
return 0;
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
using namespace std;
#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main(){
int N;
while(cin>>N && N){
vector<int> l(N);
REP(i, N) cin>>l[i];
int M = 256;
int as, aa, ac;
double ent = 1e32;
REP(S, 16)REP(A, 16)REP(C, 16){
vector<int> R(N + 1);
R[0] = S;
REP(i, N) R[i + 1] = (A * R[i] + C) % M;
REP(i, N) R[i + 1] = (R[i + 1] + l[i]) % M;
double cnt[256] = {};
REP(i, N) cnt[R[i + 1]] += 1.0;
double e = 0;
REP(i, 256)if(cnt[i] != 0){
double c = cnt[i];
e += -c/N * log(c/N);
if(!(e + EPS < ent)) goto END;
}
ent = e;
as = S, aa = A, ac = C;
END:;
}
printf("%d %d %d\n", as, aa, ac);
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
constexpr double eps = 1e-10;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
while (cin >> n, n) {
vector<int> a(n);
for (auto&& e : a) {
cin >> e;
}
double mn = numeric_limits<double>::max();
int rs = -1, rp = -1, rq = -1;
for (int s = 0; s <= 15; ++s) {
for (int p = 0; p <= 15; ++p) {
for (int q = 0; q <= 15; ++q) {
vector<int> c(256);
int r = s;
for (int i = 0; i < n; ++i) {
r = (p * r + q) & 0xff;
++c[(a[i] + r) & 0xff];
}
double cur = 0;
for (double e : c) {
if (e > eps) {
cur -= (e / n) * log(e / n);
}
}
if (cur + eps < mn) {
mn = cur;
rs = s, rp = p, rq = q;
}
}
}
}
cout << rs << ' ' << rp << ' ' << rq << '\n';
}
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
using namespace std;
#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main(){
int N;
while(cin>>N && N){
vector<int> l(N);
REP(i, N) cin>>l[i];
int MAP = (1<<8) - 1;
int as, aa, ac;
double ent = 1e32;
REP(S, 16)REP(A, 16)REP(C, 16){
vector<int> R(N + 1);
R[0] = S;
REP(i, N){
R[i + 1] = (A * R[i] + C) & MAP;
}
REP(i, N){
R[i + 1] = (R[i + 1] + l[i]) & MAP;
}
double cnt[256] = {};
REP(i, N) cnt[R[i + 1]] += 1.0;
double e = 0;
REP(i, 256)if(cnt[i] != 0){
double c = cnt[i];
e += -c * log(c/N);
if(!(e + EPS < ent)) goto END;
}
ent = e;
as = S, aa = A, ac = C;
END:;
}
printf("%d %d %d\n", as, aa, ac);
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
const int M = 256;
const double epsilon = 1e-9;
int main() {
int N;
while (cin >> N && N) {
vector<int> L;
for (auto i=0; i<N; i++) {
int l;
cin >> l;
L.push_back(l);
}
double minH = 1e9;
int minS, minA, minC;
for (auto S=0; S<16; S++) {
for (auto A=0; A<16; A++) {
for (auto C=0; C<16; C++) {
int R[300];
R[0] = S;
for (auto i=1; i<=N; i++) {
R[i] = (A*R[i-1]+C)%M;
// cerr << R[i] << endl;
}
int O[300];
int App[300];
fill(App, App+300, 0);
for (auto i=0; i<N; i++) {
O[i] = (R[i+1] + L[i])%M;
// if (S == 1 && A == 5 && C == 11) cerr << O[i] << endl;
App[O[i]]++;
}
double H = 0;
for (auto i=0; i<300; i++) {
double temp = (double)App[i]/N;
// cerr << "temp = " << temp;
if (temp != 0) H -= temp * log(temp);
}
// cerr << H << endl;
if (H + epsilon < minH) {
minH = H;
minS = S;
minA = A;
minC = C;
// cerr << "S = " << minS << ", A = " << minA
// << ", C = " << minC << ", H = " << H << endl;
}
}
}
}
cout << minS << " " << minA << " " << minC << endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int R[260], I[260], O[260];
int s, a, c, n, m = 256;
int ans[3];
double H, min_H;
int main(void){
int i, j;
while(cin >> n, n){
for(i = 1; i <= n; i++) cin >> I[i];
min_H = 100;
for(s = 0; s <= 15; s++){
for(a = 0; a <= 15; a++){
for(c = 0; c <= 15; c++){
H = 0;
memset(R, 0, sizeof(R));
memset(O, 0, sizeof(O));
R[0] = s;
for(i = 1; i <= n; i++){
R[i] = (a * R[i-1] + c) % m;
O[i] = (I[i] + R[i]) % m;
}
int cnt[256] = {0};
for(i = 1; i <= n; i++) cnt[O[i]]++;
for(i = 0; i < 256; i++){
if(!cnt[i]) continue;
H -= cnt[i] * log(cnt[i]);
}
if(min_H > H){
ans[0] = s; ans[1] = a; ans[2] = c;
min_H = H;
}
}
}
}
cout << ans[0] << " " << ans[1] << " " << ans[2] << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i=(a);i<(b);i++)
#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)
#define REP(i,n) for (ll i=0;i<(n);i++)
#define RREP(i,n) for (ll i=(n)-1;i>=0;i--)
#define debug(x) cout<<#x<<": "<<x<<endl
#define pb push_back
#define ALL(a) (a).begin(),(a).end()
const ll linf = 1e18;
const int inf = 1e9;
const ld eps = 1e-12;
const double pi = acos(-1);
template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
EACH(x,vec) is >> x;
return is;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
REP(i,vec.size()) {
if (i) os << " ";
os << vec[i];
}
return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector< vector<T> >& vec) {
REP(i,vec.size()) {
if (i) os << endl;
os << vec[i];
}
return os;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int N;
const int M = 256;
while (cin >> N, N) {
vector<int> I(N); cin >> I;
int as = -1, aa = -1, ac = -1;
ld ans = linf;
REP(S, 16) REP(A, 16) REP(C, 16) {
vector<int> R(N+1, S);
FOR(i, 1, N+1) R[i] = (A * R[i-1] + C) % M;
vector<int> O(N);
REP(i,N) O[i] = (I[i] + R[i+1]) % M;
map<int, int> m;
REP(i,N) m[O[i]]++;
ld H = 0;
EACH(p,m) {
ld per = (ld)p.second/N;
H -= per * log(per);
}
if (S == 1 && A == 7 && C == 12) {
// cout << m.size() << " " << O << " " << H << " " << ans << endl;
}
if (S == 8 && A == 7 && C == 14) {
// cout << m.size() << " " << O << " " << H << " " << ans << endl;
}
if (H < ans-eps) {
ans = H;
as = S, aa = A, ac = C;
}
}
cout << as << " " << aa << " " << ac << endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
double entropy(int N, const vector<int> &x) {
double H = 0;
for (int i=0; i<256; ++i) {
if (x[i] != 0) {
H -= (double)x[i] / N * log((double)x[i] / N);
}
}
return H;
}
int main() {
ios::sync_with_stdio(false);
int N;
while (cin >> N && N) {
vector<int> I(N+1);
for (int i=1; i<=N; ++i) cin >> I[i];
int M = 256;
double Hmin = 100000.0;
vector<int> ans(3);
for (int S=0; S<=15; ++S) {
for (int A=0; A<=15; ++A) {
for (int C=0; C<=15; ++C) {
vector<int> x(256, 0);
int R = S;
int O;
int cnt = 0;
int upper = N;
for (int i=1; i<=N; ++i) {
R = (A * R + C) % M;
O = (I[i] + R) % M;
cnt += x[O] == 0;
x[O] ++;
}
if (upper < cnt) continue;
upper = cnt;
double H = entropy(N, x);
if (H + 1e-8 < Hmin) {
Hmin = H;
ans[0] = S;
ans[1] = A;
ans[2] = C;
}
}
}
}
cout << ans[0] << " " << ans[1] << " " << ans[2] << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cassert>
#include<iomanip>
#define M 256
#define MAX 257
#define EPS (1e-7)
#define equals(a,b) (fabs((a)-(b)) < EPS)
using namespace std;
int N;
int I[MAX],O[MAX],R[MAX];
int cnt[MAX];
void makeR(int S,int A,int C)
{
R[0] = S;
for(int i=1;i<=N;i++)
R[i] = (A*R[i-1]+C)%M;
}
void makeO(int S,int A,int C)
{
for(int i=0;i<N;i++)
O[i] = (I[i]+R[i+1])%M;
}
double getH()
{
double ret = 0;
for(int i=0;i<MAX;i++)
{
if(cnt[i] == 0)continue;
if(cnt[i] == N)continue;
ret += (cnt[i]/(double)N)*log(cnt[i]/(double)N);
}
return -ret;
}
int main()
{
while(cin >> N,N)
{
for(int i=0;i<N;i++)cin >> I[i];
int aS,aA,aC;
aS=aA=aC=0;
double men = (1<<29);
for(int S=0;S<16;S++)
{
for(int A=0;A<16;A++)
{
for(int C=0;C<16;C++)
{
makeR(S,A,C);
makeO(S,A,C);
for(int l=0;l<MAX;l++)cnt[l]=0;
for(int i=0;i<N;i++)cnt[O[i]]++;
double H = getH();
if(men > H && !equals(men,H))
{
men = H;
aS = S;
aA = A;
aC = C;
}
}
}
}
cout << aS << " " << aA << " " << aC << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main(){
while(true){
int n;
cin >> n;
if(n == 0){ break; }
vector<int> v(n);
for(int i = 0; i < n; ++i){ cin >> v[i]; }
int best_s = 0, best_a = 0, best_c = 0;
double best_score = 1e20;
const int MOD = 256;
for(int s = 0; s <= 15; ++s){
for(int a = 0; a <= 15; ++a){
for(int c = 0; c <= 15; ++c){
int r = s;
vector<int> occur(MOD);
for(int i = 0; i < n; ++i){
r = (a * r + c) % MOD;
occur[(v[i] + r) % MOD]++;
}
double score = 0.0;
for(int i = 0; i < MOD; ++i){
if(occur[i] == 0){ continue; }
double t = static_cast<double>(occur[i]) / n;
score -= t * log(t);
}
if(score + 1e-9 < best_score){
best_s = s; best_a = a; best_c = c;
best_score = score;
}
}
}
}
cout << best_s << " " << best_a << " " << best_c << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
public class Main {
static FastScanner sc = new FastScanner();
public static void main(String[] args) throws Exception {
int[] I = new int[256];
double[] en = new double[257];
StringBuilder sb = new StringBuilder();
while (true) {
int N = sc.nextInt();
if (N == 0) break;
for (int i = 0; i < N; ++i) {
double v = 1.0 * (i + 1) / N;
en[i + 1] = -v * Math.log(v);
I[i] = sc.nextInt();
}
double best = Double.MAX_VALUE;
int bs = 0;
int ba = 0;
int bc = 0;
for (int s = 0; s < 16; ++s) {
for (int a = 0; a < 16; ++a) {
OUT: for (int c = 0; c < 16; ++c) {
int[] hist = new int[256];
int r = s;
for (int i = 0; i < N; ++i) {
r = (a * r + c) & 0xFF;
hist[(I[i] + r) & 0xFF]++;
}
double e = 0;
for (int i = 0; i < 256; ++i) {
if (hist[i] != 0) {
e += en[hist[i]];
if (e >= best) continue OUT;
}
}
if (e < best - 1e-10) {
best = e;
bs = s;
ba = a;
bc = c;
}
}
}
}
sb.append(bs + " " + ba + " " + bc + "\n");
}
System.out.print(sb);
}
static class FastScanner {
Reader input;
FastScanner() {
this.input = new BufferedReader(new InputStreamReader(System.in));
}
int nextInt() throws IOException {
int sign = 1;
int b = input.read();
while ((b < '0' || '9' < b) && b != '-' && b != '+') {
b = input.read();
}
if (b == '-') {
sign = -1;
b = input.read();
} else if (b == '+') {
b = input.read();
}
int ret = b - '0';
while (true) {
b = input.read();
if (b < '0' || '9' < b) return ret * sign;
ret *= 10;
ret += b - '0';
}
}
}
} | JAVA |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<iostream>
#include<map>
#include<cmath>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n) REP(i,0,n)
const int DIV =256;
double calc(int S,int A,int C,int *I,int n){
int R[n+1];
int O[n];
map<int,int>M;
R[0]=S;
REP(i,1,n+1)R[i]=(A*R[i-1]+C)%DIV;
rep(i,n)O[i]=(I[i]+R[i+1])% DIV,M[O[i]]++;
double ret=0;
map<int,int>::iterator itr = M.begin();
while(itr != M.end()){
int t=(*itr).second;
double tmp=(double)t/(double)n;
ret+= -tmp*log(tmp);
itr++;
}
return ret;
}
void solve(int n,int *I){
double ans = 1e300;
int a=100,s=100,c=100;
rep(S,16){
rep(A,16){
rep(C,16){
double tmp = calc(S,A,C,I,n);
if ( tmp+1e-10 < ans){
ans=tmp;
a=A;s=S;c=C;
}
else if( abs(tmp-ans)<1e-10){
if ( S<s){
a=A;s=S;c=C;
ans=tmp;
}else if ( S == s){
if ( A < a){
a=A;s=S;c=C;ans=tmp;
}else if ( a == A){
if (C<c){a=A;s=S;c=C;ans=tmp;}
}
}
}
}
}
}
cout <<s << " " << a << " " << c << endl;
}
int main(){
int n;
while(cin>>n &&n){
int a[n];
rep(i,n)cin>>a[i];
solve(n,a);
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
#define M 256;
using namespace std;
typedef pair <int,int> P;
typedef pair <int,P> PP;
int main(){
while(1){
int n,l[258],R[258],O[258];
cin>>n;
if(!n)break;
for(int i=1;i<=n;i++)cin>>l[i];
double ans=1e9;
PP SAC;
for(int s=0;s<=15;s++)
for(int a=0;a<=15;a++)
for(int c=0;c<=15;c++){
R[0]=s;
for(int i=1;i<=n;i++) R[i]=(a*R[i-1]+c)%M;
for(int i=1;i<=n;i++) O[i]=(l[i]+R[i])%M;
int cnt[258]={};
for(int i=1;i<=n;i++)cnt[O[i]]++;
double H=0;
for(int i=0;i<256;i++)
if(cnt[i])H-=(1.0*cnt[i]/n)*log(1.0*cnt[i]/n);
if(ans-(1e-9)>H) ans=H,SAC=PP(s,P(a,c));
if(fabs(ans-H)<=0.0000001) SAC=min(SAC,PP(s,P(a,c)));
}
cout <<SAC.first<<" "<<SAC.second.first<<" "<<SAC.second.second<<endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
using namespace std;
#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main(){
int N;
while(cin>>N && N){
vector<int> l(N);
REP(i, N) cin>>l[i];
int M = 256;
int as, aa, ac;
double ent = 1e32;
REP(S, 16)REP(A, 16)REP(C, 16){
vector<int> R(N + 1);
R[0] = S;
REP(i, N){
R[i + 1] = (A * R[i] + C) % M;
}
REP(i, N){
R[i + 1] += l[i];
if(R[i + 1] >= M) R[i + 1] -= M;
}
double cnt[256] = {};
REP(i, N) cnt[R[i + 1]] += 1.0;
double e = 0;
REP(i, 256)if(cnt[i] != 0){
double c = cnt[i];
e += -c/N * log(c/N);
if(!(e + EPS < ent)) goto END;
}
ent = e;
as = S, aa = A, ac = C;
END:;
}
printf("%d %d %d\n", as, aa, ac);
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
using namespace std;
#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main(){
int N;
while(cin>>N && N){
vector<int> l(N);
REP(i, N) cin>>l[i];
int M = 256;
int as, aa, ac;
double ent = 1e32;
REP(S, 16)REP(A, 16)REP(C, 16){
vector<int> R(N + 1);
R[0] = S;
REP(i, N) R[i + 1] = (A * R[i] + C) % M;
REP(i, N) R[i + 1] = (R[i + 1] + l[i]) % M;
map<int, int> cnt;
REP(i, N) cnt[R[i + 1]] += 1;
double e = 0;
FORIT(it, cnt){
double c = it->second;
e += -c/N * log(c/N);
}
if(e + EPS < ent){
ent = e;
as = S, aa = A, ac = C;
}
}
printf("%d %d %d\n", as, aa, ac);
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main(void){
while (1) {
int N;
cin >> N;
if (N == 0) break;
int l[N];
for (int i = 0; i < N; i++) cin >> l[i];
double H = 1e10;
int s,a,c;
for (int S = 0; S < 16; S++) {
for (int A = 0; A < 16; A++) {
for (int C = 0; C < 16; C++) {
int R = S;
int L[256];
for (int i = 0; i < 256; i++) L[i] = 0;
for (int i = 0; i < N; i++) {
R = (A*R+C)%256;
L[(l[i]+R)%256] += 1;
}
double tmp = 0.0;
for (int i = 0; i < 256; i++) {
if (L[i] > 0) {
tmp += -(double)L[i]/N*log2((double)L[i]/N);
}
}
if (tmp < H) {
H = tmp;
s = S; a = A; c = C;
}
}
}
}
printf("%d %d %d\n",s,a,c);
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<bits/stdc++.h>
#define MOD 256
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
int I[256];
int main() {
int n;
while (scanf("%d", &n), n) {
rep(i, n)scanf("%d", &I[i]);
int S, A, C;
double Min = INFINITY;
rep(s, 16)rep(a, 16)rep(c, 16) {
int r = s;
int mp[256]{};
rep(i, n) {
r = (a*r + c) % MOD;
mp[(I[i] + r) % MOD]++;
}
double sum = 0;
rep(i, 256) {
if (!mp[i])continue;
double d = mp[i] / double(n);
sum -= d*log10(d);
}
if (Min > sum + 1e-10) {
Min = sum; S = s; A = a; C = c;
}
}
printf("%d %d %d\n", S, A, C);
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>
#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()
int dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
typedef vector<vector<int> > VVI;
const int INF = 1e9;
const double EPS = 1e-6;
int MOD = 256;
double calc(vector<int> &I, int N, int S, int A, int C)
{
int R = S;
double res = 0;
vector<int> occur(256);
for (int i=0; i<N; i++) {
R = (A*R + C) % MOD;
occur[(I[i]+R)%MOD]++;
}
for (int i=0; i<256; i++) {
if (occur[i]>0) {
res -= (double)occur[i]/N*log((double)occur[i]/N);
}
}
return res;
}
int main()
{
int N;
while (cin>>N, N) {
vector<int> I(N);
rep(i, N) cin>>I[i];
double min_e = INF;
int ans[3] = {};
for (int S=0; S<16; S++) {
for (int A=0; A<16; A++) {
for (int C=0; C<16; C++) {
double now = calc(I, N, S, A, C);
if (min_e - EPS> now) {
min_e = now;
ans[0] = S, ans[1] = A, ans[2] = C;
}
}
}
}
cout << ans[0] << " " << ans[1] << " " << ans[2] << endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<29;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-11;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
int main(){
while(1){
int n;
cin>>n;
if(n==0) break;
vi a(n);
for(int i=0;i<n;i++) cin>>a[i];
double mn=inf;
int A,B,C;
for(int i=0;i<=15;i++) for(int j=0;j<=15;j++) for(int k=0;k<=15;k++){
vi b(256);
int t=i;
for(int l=0;l<n;l++){
t=(j*t+k)%256;
b[(a[l]+t)%256]++;
}
double tmp=0;
for(int l=0;l<256;l++) if(b[l]) tmp-=log((double)b[l]/n)*b[l];
if(tmp+eps<mn){
A=i,B=j,C=k;
mn=tmp;
}
}
cout<<A<<' '<<B<<' '<<C<<endl;
}
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;
int n;
int M = 256;
ld entropy(const vector<int> &v)
{
ld sum = 0;
map<int, ld> mp;
for (int i = 1; i <= n; i++)
{
mp[v[i]]++;
}
for (auto itr = mp.begin(); itr != mp.end(); itr++)
{
sum -= itr->second / n * log(itr->second / n);
}
return sum;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
while (cin >> n, n)
{
vector<int> R(n + 1), I(n + 1), O(n + 1);
for (int i = 1; i <= n; i++)
cin >> I[i];
int S, A, C;
ld mi = INF;
for (int s = 0; s <= 15; s++)
{
for (int a = 0; a <= 15; a++)
{
for (int c = 0; c <= 15; c++)
{
R[0] = s;
for (int i = 1; i <= n; i++)
{
R[i] = (a * R[i - 1] + c) % M;
O[i] = (I[i] + R[i]) % M;
}
ld e = entropy(O);
if (e + EPS < mi){
mi = e;
S = s;
A = a;
C = c;
}
}
}
}
cout << S << " " << A << " " << C << endl;
}
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstring>
#include <string>
#include <sstream>
#include <bitset>
using namespace std;
#define INF 100000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define SS stringstream
#define rep(i,n) for(int i = 0; i < n; i++)
#define SORT(x) sort((x).begin(), (x).end())
#define clr(a,b) memset((a),(b),sizeof(a))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<P> Vp;
typedef priority_queue<P, vector<P>, greater<P> > PQ;
int main(){
int n;
while(scanf("%d", &n),n){
int t[300], r[300], num[300];
long double ent = 100000000.0;
int ss, aa, cc;
rep(i,n) scanf("%d", &t[i]);
for(int s = 0; s <= 15; s++){
for(int a = 0; a <= 15; a++){
for(int c = 0; c <= 15; c++){
long double tent = 0.0;
rep(i,300) num[i] = 0;
r[0] = s;
for(int i = 1; i < 300; i++) r[i] = (a*r[i-1]+c)%256;
rep(i,299) r[i] = r[i+1];
rep(i,n) num[(t[i]+r[i])%256]++;
rep(i,300){
if(num[i] == 0) continue;
long double tmp = (long double)log((double)num[i])-(long double)log((double)n);
tent -= (long double)num[i]*tmp;
}
if(tent < ent){
ent = tent;
ss = s;
aa = a;
cc = c;
}
}
}
}
printf("%d %d %d\n", ss, aa, cc);
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
static const double EPS = 1e-9;
static const int MOD = 256;
int I[257];
int R[257];
int O[257];
int main(){
int N;
while(cin >> N && N){
for(int i = 1; i <= N; i++){
cin >> I[i];
}
int ansS, ansA, ansC;
double ansH = 100000000.0;
for(int S = 0; S <= 15; S++){
for(int A = 0; A <= 15; A++){
for(int C = 0; C <= 15; C++){
double H = 0;
R[0] = S;
memset(O, 0, sizeof(O));
for(int i = 1; i <= N; i++){
R[i] = (A * R[i - 1] + C) % MOD;
O[ (I[i] + R[i]) % MOD ]++;
}
for(int i = 0; i < 256; i++){
if(O[i] != 0) H -= (O[i] / (double)(N) * log(O[i] / (double)(N)));
}
if(H + EPS < ansH){
ansH = H;
ansS = S;
ansA = A;
ansC = C;
}
}
}
}
cout << ansS << ' ' << ansA << ' ' << ansC << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<bits/stdc++.h>
#define MOD 256
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
int I[256];
int main() {
int n;
while (scanf("%d", &n), n) {
rep(i, n)scanf("%d", &I[i]);
int S, A, C;
double Min = INFINITY;
rep(s, 16)rep(a, 16)rep(c, 16) {
int r = s;
unordered_map<int, int>mp;
rep(i, n) {
r = (a*r + c) % MOD;
mp[(I[i] + r) % MOD]++;
}
double sum = 0;
for (auto p : mp) {
double d = p.second / double(n);
sum -= d * log10(d);
}
if (Min > sum + 1e-10) {
Min = sum; S = s; A = a; C = c;
}
}
printf("%d %d %d\n", S, A, C);
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <tuple>
#include <queue>
#include <set>
#include <cstdio>
#include <climits>
#include <cmath>
#include <array>
#include <functional>
#include <sstream>
#include <list>
#include <set>
const int MOD=1000000007;
const int INF=1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps=1e-9;
const int inf=1e9;
typedef pair<int,int> P;
int buckets[256];
int main(int argc,char const* argv[])
{
while(1)
{
int n,trues,truea,truec;
int m=256;
cin >> n;
if(!n)break;
vi l(n+5);
vi r(n+5);
vi o(n+5);
for(int i=1;i<=n;i++)
{
cin >> l[i];
}
double ans=1000;
for(int s=0;s<=15;s++)
{
for(int a=0;a<=15;a++)
{
for(int c=0;c<=15;c++)
{
memset(buckets,0,sizeof(buckets));
for(int i=0;i<=n;i++)
{
if(i==0) r[i]=s;
else
{
r[i]=(a*r[i-1]+c)%m;
o[i]=(l[i]+r[i])%m;
buckets[o[i]]++;
}
}
double tmp=0;
for(int i=0;i<=255;i++)
{
if(buckets[i]!=0)
{
tmp-=buckets[i]/(double)n*log(buckets[i]/(double)n);
}
}
if(tmp+eps<ans)
{
ans=tmp;
trues=s;
truea=a;
truec=c;
}
}
}
}
cout << trues << ' ' << truea << ' ' << truec << endl;
}
return 0;
}
| CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
public class Main {
static FastScanner sc = new FastScanner();
public static void main(String[] args) throws Exception {
int[] I = new int[256];
double[] en = new double[257];
StringBuilder sb = new StringBuilder();
while (true) {
int N = sc.nextInt();
if (N == 0) break;
for (int i = 0; i < N; ++i) {
double v = 1.0 * (i + 1) / N;
en[i + 1] = -v * Math.log(v);
I[i] = sc.nextInt();
}
double best = Double.MAX_VALUE;
int bs = 0;
int ba = 0;
int bc = 0;
for (int s = 0; s < 16; ++s) {
for (int a = 0; a < 16; ++a) {
OUT: for (int c = 0; c < 16; ++c) {
int[] hist = new int[256];
int r = s;
for (int i = 0; i < N; ++i) {
r = (a * r + c) & 0xFF;
hist[(I[i] + r) & 0xFF]++;
}
double e = 0;
for (int i = 0; i < 256; ++i) {
if (hist[i] != 0) {
e += en[hist[i]];
if (e > best) continue OUT;
}
}
if (e < best - 1e-10) {
best = e;
bs = s;
ba = a;
bc = c;
}
}
}
}
sb.append(bs + " " + ba + " " + bc + "\n");
}
System.out.print(sb);
}
static class FastScanner {
Reader input;
FastScanner() {
this.input = new BufferedReader(new InputStreamReader(System.in));
}
int nextInt() throws IOException {
int sign = 1;
int b = input.read();
while ((b < '0' || '9' < b) && b != '-' && b != '+') {
b = input.read();
}
if (b == '-') {
sign = -1;
b = input.read();
} else if (b == '+') {
b = input.read();
}
int ret = b - '0';
while (true) {
b = input.read();
if (b < '0' || '9' < b) return ret * sign;
ret *= 10;
ret += b - '0';
}
}
}
} | JAVA |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
while(cin>>n,n) {
int M = 256;
int I[n]; FOR(i,0,n) cin >> I[i];
int ans[3];
double mn = 1e18;
FOR(S,0,16) {
FOR(A,0,16) {
FOR(C,0,16) {
int R[n+1];
R[0] = S;
FOR(i,1,n+1) R[i] = (A * R[i-1] + C) % M;
int O[n];
FOR(i,0,n) O[i] = (I[i] + R[i+1]) % M;
double H = 0;
int num[300]; CLR(num);
FOR(i,0,n) num[O[i]]++;
FOR(i,0,300) {
if(num[i] == 0) continue;
H += num[i] * log((double)num[i]);
}
H = -H;
if(H < mn) {
ans[0] = S;
ans[1] = A;
ans[2] = C;
mn = H;
}
}
}
}
FOR(i,0,3) {
if(i) cout << " ";
cout << ans[i];
}
cout << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
using namespace std;
#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main(){
int N;
while(cin>>N && N){
vector<int> l(N);
REP(i, N) cin>>l[i];
int M = 256;
int as, aa, ac;
double ent = 1e32;
REP(S, 16)REP(A, 16)REP(C, 16){
vector<int> R(N + 1);
R[0] = S;
REP(i, N){
R[i + 1] = (A * R[i] + C) % M;
while(R[i + 1] >= M) R[i + 1] -= M;
}
REP(i, N){
R[i + 1] += l[i];
while(R[i + 1] >= M) R[i + 1] -= M;
}
double cnt[256] = {};
REP(i, N) cnt[R[i + 1]] += 1.0;
double e = 0;
REP(i, 256)if(cnt[i] != 0){
double c = cnt[i];
e += -c/N * log(c/N);
if(!(e + EPS < ent)) goto END;
}
ent = e;
as = S, aa = A, ac = C;
END:;
}
printf("%d %d %d\n", as, aa, ac);
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;
#define ll long long
#define vvi vector< vector<int> >
#define vi vector<int>
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int I[300],R[300],O[300];
int main(){
int N;
const int MOD = 256;
while(1){
cin >> N;
if(N==0) break;
FOR(i,1,N+1) cin >> I[i];
int ansS,ansA,ansC;
double ansH = 100000000.0;
REP(A,16){REP(S,16){REP(C,16){
double H = 0;
REP(i,300) O[i] = 0;
R[0] = S;
for(int i = 1; i <= N; i++){
R[i] = (A * R[i - 1] + C) % MOD;
O[ (I[i] + R[i]) % MOD ]++;
}
for(int i = 0; i < 256; i++){
if(O[i] != 0) H -= (O[i] / (double)(N) * log(O[i] / (double)(N)));
}
if(H + 1e-11 < ansH){
ansH = H;
ansS = S;
ansA = A;
ansC = C;
}
}
}
}
cout << ansS << " " << ansA << " " << ansC << endl;
}
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<iostream>
#include<vector>
#include<tuple>
#include<algorithm>
#include<cmath>
using namespace std;
int n, I[1 << 10], R[1 << 10], O[1 << 10], V[1 << 10];
vector<tuple<long double, int, int, int>>vec;
int main() {
while (true) {
cin >> n; vec.clear(); if (n == 0)break;
for (int i = 1; i <= n; i++) { cin >> I[i]; }
for (int S = 0; S < 16; S++) {
for (int A = 0; A < 16; A++) {
for (int C = 0; C < 16; C++) {
R[0] = S; long double sum = 0.0l;
for (int i = 1; i <= n; i++) { R[i] = (A*R[i - 1] + C) % 256; }
for (int i = 1; i <= n; i++) { O[i] = (I[i] + R[i]) % 256; }
for (int i = 0; i < 256; i++) { V[i] = 0; }
for (int i = 1; i <= n; i++) { V[O[i]]++; }
for (int i = 0; i < 256;i++){
if (V[i] >= 1) {
long double F = 1.0*V[i] / n;
sum -= F*log(F);
}
}
vec.push_back(make_tuple(sum, S, A, C));
}
}
}
sort(vec.begin(), vec.end());
cout << get<1>(vec[0]) << ' ' << get<2>(vec[0]) << ' ' << get<3>(vec[0]) << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<cstdio>
#include<cmath>
#define R(i,n) for(int i=0;i<n;i++)
#define D double
using namespace std;
int main(){int n,l[256],x,y,z;while(scanf("%d",&n),n){R(i,n)scanf("%d",&l[i]);D m=1e9;R(s,16)R(a,16)R(c,16){int u[256]={0};int r=s;R(i,n){r = (a*r + c)&255;u[(r + l[i])&255]++;}D tmp=0;R(i,256)if(u[i]){tmp-=u[i]*log((D)u[i]/n);if(m+1e-9<tmp)break;}if(m>tmp+1e-9){m=tmp;x=s;y=a;z=c;}}printf("%d %d %d\n",x,y,z);}} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <cmath>
using namespace std;
#define MOD (256)
const double eps = 1e-10;
int main(void)
{
int n;
while (cin >> n, n){
int I[n];
for (int i = 0; i < n; i++){
cin >> I[i];
}
double ent = 1 << 29;
int S = -1, A = -1, C = -1;
for (int s = 0; s < 16; s++){
for (int a = 0; a < 16; a++){
for (int c = 0; c < 16; c++){
int R = s;
int O[300] = {0};
for (int i = 0; i < n; i++){
R = (a * R + c) % MOD;
O[(I[i] + R) % MOD]++;
}
double tmp = 0;
for (int i = 0; i < 256; i++){
if (0 < O[i]) tmp -= O[i] * 1.0 / n * log(O[i] * 1.0 / n);
}
if (tmp + eps < ent){
ent = tmp;
S = s;
A = a;
C = c;
}
}
}
}
cout << S << " " << A << " " << C << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include "bits/stdc++.h"
#define REP(i,n) for(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),(v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(i, (v).size()) { cout << v[i]; if (i != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9
const int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
const int dy[8] = { 0,1,1,1,0,-1,-1,-1 };
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
///(?Β΄????????`)(?Β΄????????`)(?Β΄????????`)(?Β΄????????`)(?Β΄????????`)(?Β΄????????`)///
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (1) {
int n;
cin >> n;
if (!n)break;
vi l(n);
REP(i, n)cin >> l[i];
vi r(n+1);
vi v(n);
vi o(256);
double ans = INF*INF;
int x = 0,y = 0,z = 0;
REP(s, 16) {
REP(a, 16) {
REP(c, 16) {
REP(i, 256)o[i] = 0;
r[0] = s;
REP(i, n) {
r[i + 1] = (a*r[i] + c) % 256;
}
REP(i, n)v[i] = (r[i+1] + l[i]) % 256;
REP(i, n) {
o[v[i]]++;
}
double tmp = 0;
REP(i, 256) {
double d = double(o[i]) / n;
if (o[i] != 0)tmp -= d*log(d);
}
if (tmp+EPS < ans){
x = s, y = a, z = c;
ans = tmp;
}
}
}
}
cout << x << " " << y << " " << z << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
#define INF 999999999
#define REP(i,n) for(int i=0; i<(int)(n); i++)
int main(){
int n, o[256], in[256], x[256], s, a, c, r, ms, ma, mc;
double ans, ins;
while(1){
cin >> n;
if(!n) break;
ans = 999;
REP(i,n) cin >> in[i];
REP(s,16){
REP(a,16){
REP(c,16){
REP(i,256) x[i]=0;
r=s;
REP(i,n){
r = (r*a+c) % 256;
o[i] = (in[i]+r)%256;
x[o[i]]++;
//cout << o[i] << " ";
}
//cout << endl;
ins = 0;
REP(i,256)if(x[i]>0)ins += -((double)x[i]/(double)n) * log2((double)x[i]/(double)n);
//cout << "H:" << ins << endl;
if(ans>ins){
ms = s;
ma = a;
mc = c;
ans = ins;
}
}
}
}
cout << ms << " " << ma << " " << mc << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 |
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main {
Scanner sc = new Scanner(System.in);
void run() {
for (;;) {
int n = sc.nextInt();
if (n == 0) {
break;
}
int l[] = new int[n+1];
for (int i = 1; i < n+1; i++) {
l[i] = sc.nextInt();
}
int res = 0;
int rea = 0;
int rec = 0;
double max = -Double.MAX_VALUE;
BigInteger min = null;
for (int s = 0; s <= 15; s++) {
for (int a = 0; a <= 15; a++) {
for (int c = 0; c <= 15; c++) {
boolean spd = false;
if(s == 8 && a==7 && c == 14){
// spd = true;
}
if(s == 1 &&a == 7 &&c==12){
// spd = true;
}
int hash[] = new int[256];
int[] r = new int[n + 1];
r[0] = s;
for (int i = 0; i < n; i++) {
if(spd){
System.out.println((r[i]+l[i])&255);
}
r[i + 1] = (a * r[i] + c) & (255);
hash[(r[i+1]+l[i+1])&255 ]++;
}
BigInteger bi = BigInteger.ONE;
for (int i = 0; i < 256; i++) {
if (hash[i] != 0) {
bi = bi.multiply(BigInteger.valueOf(hash[i]).pow(hash[i]));
}
}
if(min == null){
min = bi;
}
if(spd){
System.out.println(bi);
}
if(min.compareTo(bi)<0){
min = bi;
res = s;
rea = a;
rec = c;
}
}
}
}
System.out.println(res+" "+rea+" "+rec);
}
}
public static void main(String[] args) {
Main m = new Main();
m.run();
}
} | JAVA |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | #include <iostream>
#include <cmath>
using namespace std;
int main() {
const int M = 256;
int N;
while (cin >> N && N) {
int I[256];
for (int i=1; i<=N; ++i) cin >> I[i];
double Hmin = 1000.0;
int s, a, c, upper = N;
for (int S=0; S<=15; ++S) {
for (int A=0; A<=15; ++A) {
for (int C=0; C<=15; ++C) {
int x[256] = {0};
int R = S;
int O;
int cnt = 0;
for (int i=1; i<=N; ++i) {
R = (A * R + C) % M;
O = (I[i] + R) % M;
cnt += x[O] == 0;
if (upper < cnt) break;
x[O] ++;
}
if (cnt <= upper) {
upper = cnt;
double H = 0;
for (int i=0; i<256; ++i) {
if (x[i]) H -= (double)x[i] / N * log((double)x[i] / N);
}
if (H + 1e-9 < Hmin) {
Hmin = H;
s = S;
a = A;
c = C;
}
}
}
}
}
cout << s << " " << a << " " << c << endl;
}
return 0;
} | CPP |
p01283 Strange String Manipulation | A linear congruential generator produces a series R(β
) of pseudo-random numbers by the following for- mulas:
<image>
where S, A, C, and M are all parameters. In this problem, 0 β€ S, A, C β€ 15 and M = 256.
Now suppose we have some input string I(β
), where each character in the string is an integer between 0 and (M - 1). Then, using the pseudo-random number series R(β
), we obtain another string O(β
) as the output by the following formula:
<image>
Your task is to write a program that shows the parameters S, A, and C such that the information entropy of the output string O(β
) is minimized. Here, the information entropy H is given by the following formula:
<image>
where N is the length of the string and #(x) is the number of occurences of the alphabet x.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
I(1) I(2) ... I(N)
N does not exceed 256.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print in a line the values of the three parameters S, A, and C separated by a single space. If more than one solution gives the same minimum entropy, choose the solution with the smallest S, A, and then C.
Example
Input
5
5 4 3 2 1
5
7 7 7 7 7
10
186 8 42 24 154 40 10 56 122 72
0
Output
0 1 1
0 0 0
8 7 14 | 7 | 0 | import math
def prodR(s,a,c,N):
R=[s]
for i in range(1,N+1):
R.append((a*R[i-1]+c)%256)
return R[1:]
def entropy(R,I,N):
O=[(I[i]+R[i])%256 for i in range(N)]
L=[0]*256
for i in O:
L[i]+=1
H=-sum(float(L[i])/N*math.log(float(L[i])/N,2) for i in range(256) if L[i]!=0)
return H
while True:
N=input()
if N==0:break
I=map(int,raw_input().split())
maxInt=1e10
for s in range(16):
for a in range(16):
for c in range(16):
R=prodR(s,a,c,N)
H=entropy(R,I,N)
if maxInt>H:
maxInt=H
ans=s,a,c
print "%d %d %d"%(ans[0],ans[1],ans[2]) | PYTHON |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.