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
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<bits/stdc++.h> using namespace std; int main(){ int n,m,ans=0; pair<int,int> a[10000]; while(1){ ans=0; cin>>n>>m; if(n==0 && m==0) break; for(int i=0;i<n;i++){ cin>>a[i].second>>a[i].first; } sort(a,a+n); for(int i=n-1;i>=0;i--){ if(a[i].second<m) m-=a[i].second; else if(m>0){ ans+=(a[i].second-m)*a[i].first; m=0; } else ans+=(a[i].first)*a[i].second; } cout<<ans<<endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while 1: n,m= list(map(int,input().split(' '))) if n == m ==0:break D = [] for _ in range(n): d_i,p_i = list(map(int,input().split(' '))) D.append((d_i,p_i)) D = sorted(D,key = lambda x:x[1],reverse = True) while m > 0: if len(D) ==0:break temp = D.pop(0) if temp[0] > m: temp = (temp[0] - m,temp[1]) m=0 D.append(temp) else: m -= temp[0] ans = 0 for i in D: ans += i[0]*i[1] print(ans)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>P; int main(){ int N,M; while(1){ cin >>N>>M; if(N==0 && M==0)break; P a[N]; for(int i=0 ; i < N ; i++){ cin >>a[i].second>>a[i].first; } sort(a,a+N,greater<P>()); for(int i=0 ; i < N ; i++){ int d=min(M,a[i].second); M-=d; a[i].second-=d; } int ans=0; for(int i=0 ; i < N ; i++){ ans+=a[i].first*a[i].second; } cout <<ans<<endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while True: n,m = map(int,input().split()) if n==0: break dp = [list(map(int,input().split())) for _ in range(n)] dp.sort(key=lambda x:x[1],reverse=True) sm = 0 for d,p in dp: d,m = [d-m, 0] if d>m else [0, m-d] if m==0: sm += d*p print(sm)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while 1: n,m=map(int,input().split()) a=[] if n==0:break for _ in range(n): d,p=map(int,input().split()) a.append([p,d]) a.sort(reverse=1) b=0 for p,d in a: if d<=m: m-=d else: b+=p*(d-m) m=0 print(b)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; class Main { private static void solve() { final Scanner scanner = new Scanner(System.in); while (true) { final int n = scanner.nextInt(); final int m = scanner.nextInt(); if (n == 0 && m == 0) { break; } final int[][] data = new int[n][2]; for (int i = 0; i < n; i++) { data[i][0] = scanner.nextInt(); data[i][1] = scanner.nextInt(); } Arrays.sort(data, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return o1[1] == o2[1] ? 0 : (o1[1] < o2[1] ? 1 : -1); } }); int result = 0; int i = 0; for (int sum = 0; i < n; i++) { if (sum + data[i][0] > m) { result += (sum + data[i][0] - m) * data[i][1]; break; } else { sum += data[i][0]; } } for (i++ ; i < n; i++) { result += data[i][0] * data[i][1]; } System.out.println(result); } } public static void main(String... args) { solve(); } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; typedef pair<int, int> P; //p, d int n, m; P pd[10000]; signed main() { int i; while (cin >> n >> m) { if (!n) break; for (i = 0; i < n; i++) cin >> pd[i].second >> pd[i].first; sort(pd, pd + n, greater<P>()); int ans = 0; for (i = 0; i < n; i++) { if (m >= pd[i].second) { m -= pd[i].second; } else { ans += pd[i].first * (pd[i].second - m); m = 0; } } cout << ans << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <algorithm> #include <utility> #include <iostream> using namespace std; int N, M, ans; pair<int,int> PD[10010]; int main() { while (cin >> N >> M && N) { ans = 0; int d, p, m; m = M; for (int i=0; i<N; ++i) { cin >> d >> p; PD[i] = make_pair(p, d); } sort(PD,PD+N); for (int i=0; i<N; ++i){ if (m <= PD[N-1-i].second || m == 0){ ans += (PD[N-1-i].second-m)*PD[N-1-i].first; m = 0; }else{ m -= PD[N-1-i].second; } } cout << ans << endl; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while True: N,M = map(int,input().split()) if N == 0: break src = [] for i in range(N): d,p = map(int,input().split()) src.append((p,d)) money = M ans = 0 for p,d in sorted(src,reverse=True): guard = min(d,money) money -= guard ans += (d - guard) * p print(ans)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <vector> #include <algorithm> # define REP(i,n) for(int i=0; i<n;i++) using namespace std; int main(){ while(1){ long long int n, m, p, d, ans; vector<pair<int,int> > pd; cin >> n >> m; if (n==0 && m==0) return 0; ans = 0; REP(i,n){ cin >> d >> p; pd.push_back(make_pair(p,d)); } sort(pd.begin(), pd.end(), greater<pair<int,int> >() ); REP(i,n){ p = pd[i].first; d = pd[i].second; if (m>0){ if (m<d){ ans += (d-m)*p; } m -= d; }else{ ans += d*p; } } cout << ans << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
n,m = map(int, input().split()) while(n != 0 or m != 0): dplists = [] for i in range(n): dplist= list(map(int, input().split())) dplists.append(dplist) dplists = sorted(dplists, key = lambda x: x[1], reverse = True) for i in range(n): if ( m >= dplists[i][0]): m = m - dplists[i][0] dplists[i][0] = 0 else: dplists[i][0] = dplists[i][0] - m m = 0 l = i break sum = 0 for k in range(l,n): sum += dplists[k][0]*dplists[k][1] print(sum) n,m = map(int,input().split())
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <vector> #include <algorithm> #include <utility> using namespace std; typedef long long ll; typedef pair<ll, ll> pii; int main(){ while(true){ int N, M; cin >> N >> M; if(N == 0 && M == 0){ break; } vector<pii> r(N); for(int i = 0; i < N; ++i){ cin >> r[i].second >> r[i].first; } sort(r.begin(), r.end()); ll answer = 0; for(int i = N - 1; i >= 0; --i){ if(r[i].second <= M){ M -= r[i].second; }else{ answer += (r[i].second - M) * r[i].first; M = 0; } } cout << answer << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
# AOJ2019 shukuba = [] n = 0 m = 0 def solve(): shukuba.sort(key = lambda x: x[1]) s = sum(map(lambda x: x[0], shukuba)) - m if s <= 0: return 0 e = 0 i = 0 while s > 0: d = shukuba[i][0] if s >= shukuba[i][0] else s e += shukuba[i][1] * d s -= d i += 1 return e while True: shukuba = [] line = input() n, m = map(int, line.split()) if n == 0 and m == 0: break for _ in range(0, n): c = list(map(int, list(input().split()))) shukuba.append(c) print(solve())
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <algorithm> #include <map> #include <functional> #define fi first #define sec second using namespace std; typedef pair<int,int> P; int main(){ int n,m; while(cin >> n >> m,n||m){ P p[10000]; for(int i = 0; i < n; i++) cin >> p[i].sec >> p[i].fi; sort(p,p+n,greater<P>()); for(int i = 0; i < n; i++){ if(p[i].sec >= m){ p[i].sec -= m; break; } m -= p[i].sec; p[i].sec = 0; } int ans = 0; for(int i = 0; i < n; i++) ans += p[i].fi*p[i].sec; cout << ans << endl; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<utility> #include<algorithm> #include<iostream> using namespace std; int main() { int n, m, i, p, s; pair<int,int> w[10000]; while(cin >> n >> m && n) { for( i = 0; i < n; i++) { cin >> w[i].second >> w[i].first; } sort(w, w+n); i=1; while( i <= n ) { if ((m -= w[n-i].second) <= 0) { w[n-i].second = -m; p = n-i; break; } i++; } s = 0; if (i <= n) for(i = 0; i <= p; i++) s += w[i].first * w[i].second; cout << s << 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 <string> #include <sstream> #include <stack> #include <algorithm> #include <cmath> #include <queue> #include <map> #include <set> #include <cstdlib> #include <bitset> #include <tuple> #include <assert.h> #include <deque> #include <bitset> #include <iomanip> #include <limits> #include <chrono> #include <random> #include <array> #include <unordered_map> #include <functional> #include <complex> #include <numeric> template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int inf = 1 << 28; //constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ int kkt = 89; while (kkt) { int n; cin >> n; if (n == 0) break; vector<ll> I(n); for (int i = 0; i < n; i++) scanf("%lld", &I[i]); long double H = 1e19; vector<ll> ans(3, INF); for (int s = 0; s <= 15; s++)for (int a = 0; a <= 15; a++) for (int b = 0; b <= 15; b++) { int now = s; vector<int> cnt(256); for (int i = 0; i < n; i++) { now = (now * a + b) % 256; cnt[(now + I[i]) % 256]++; } long double tmp = 0; for (int i = 0; i <= 255; i++) { if(cnt[i] > 0) tmp -= (long double)cnt[i] / n * log10((long double)cnt[i] / n); } //cout << tmp << endl; if (tmp + 1e-14< H) { H = tmp; ans[0] = s; ans[1] = a; ans[2] = b; } else if (abs(H - tmp) < 1e-14) { //cout << s << " " << a << " " << b << endl; chmin(ans, { s,a,b }); } } cout << ans[0] << " " << ans[1] << " " << ans[2] << "\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 <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){ pair<double, tuple<int, int, int>> ans; ans = MP(1e9, 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]; } ans = min(ans, MP(calc(cnt), MT(s, a, c))); ans.first -= 1e-9; } return ans.second; } 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.ArrayList; import java.util.Scanner; public class Main { Scanner sc = new Scanner(System.in); public void run() { while(sc.hasNext()){ int n = sc.nextInt(); if(n == 0 )break; else calc(n); } } public void calc(int n){ int[] Is = new int[n]; for(int i = 0; i < n; i++){ Is[i] = sc.nextInt(); } int m = 256; int s = 16; int a = 16; int c = 16; double minH = 1000000000; for(int ns = 0; ns < 16; ns++){ for(int na = 0; na < 16; na++){ for(int nc = 0; nc < 16; nc++){ int[] count = new int[257]; int r = ns; for(int i = 0; i < n; i++){ r = (na * r + nc) % m; int o = (Is[i] + r) % m; count[o] += 1; } double h = 0; for(int i = 0; i < 257; i++){ if(count[i] != 0){ double t = ((double)count[i]) /((double) n); h += -1 * t * Math.log(t); } } if(h+1e-10 < minH){ s = ns; a = na; c = nc; minH = h; } else if(h == minH && (ns < s || (s == ns && na < a) || (s == ns && a == na && nc < c))){ s = ns; a = na; c = nc; } } } } System.out.println(s+ " " + a + " " + c); } 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 <vector> #include <list> #include <map> #include <set> #include <stack> #include <queue> #include <deque> #include <algorithm> #include <utility> #include <functional> #include <sstream> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <climits> #include <cassert> using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() #define EXIST(s,e) ((s).find(e)!=(s).end()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) const long double EPS = 1e-9; int main() { int N; const int M = 256; while (cin >> N, N) { int ansS, ansA, ansC; long double min_H = INT_MAX; vi I(N+1); REP(i, N) { cin >> I[i+1]; } REP(S, 16) { REP(A, 16) { REP(C, 16) { vi R(N+1); R[0] = S; FOR(i, 1, N+1) { R[i] = (A * R[i-1] + C) % M; } vi O(N+1); map<int, int> X; FOR(i, 1, N+1) { O[i] = (I[i] + R[i]) % M; if(EXIST(X, O[i])) { X[O[i]]++; } else { X[O[i]] = 1; } } long double H = 0; for (map<int, int>::iterator itr = X.begin(); itr != X.end(); ++itr) { long double t = (itr->second / (long double)N); H -= t * log(t); } if(H < min_H - EPS) { min_H = 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<pair<long double,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.0; 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_pair(sum, S * 256 + A * 16 + C)); } } } sort(vec.begin(), vec.end()); int T = vec[0].second; cout << T / 256 << ' ' << (T / 16) % 16 << ' ' << T % 16 << 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 rep(i,n) for(int i=0;i<n;i++) #define M 256 using namespace std; int main(){ int n,l[M],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[M] = {0}; int r = s; rep(i,n){ r = (a*r + c)%M; u[(r + l[i])%M]++; } double tmp = 0; rep(i,M)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
#include <iostream> #include <algorithm> #include <math.h> #include <vector> #include <string.h> #include <cstdio> #define M 256 int num[M]={0}; const double EPS = 1e-9; using namespace std; int main(void) { int s,a,c; int n; int R,O; double H; double mini; int mins,mina,minc; int i1,i2; while(1){ cin>>n; if(n==0) break; vector<int> I(n); for(i1=0;i1<n;i1++){ cin>>I[i1]; } mini=300.0; for(s=0;s<=15;s++){ for(a=0;a<=15;a++){ for(c=0;c<=15;c++){ memset(num,0,sizeof(num)); R=s; for(i1=0;i1<n;i1++){ R=(a*R+c)%M; O=(I[i1]+R)%M; num[O]++; } H=0.0; for(i2=0;i2<M;i2++){ if(num[i2]>0){ double x=(double)num[i2]/(double)n; H-=(x*log(x)); } } if(mini>H+EPS) { mini=H; mins=s; mina=a; minc=c; } } } } cout<<mins<<" "<<mina<<" "<<minc<<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, a, n) for(int i = a; i < n; i++) #define repb(i, a, b) for(int i = a; i >= b; i--) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl #define int long long #define first fi; #define second se; using namespace std; typedef pair<int, int> P; int r[16][16][16][260]; int mod = 256; signed main(){ int n; rep(s, 0, 16){ rep(a, 0, 16){ rep(c, 0, 16){ r[s][a][c][0] = s; rep(i, 1, 260){ r[s][a][c][i] = (a * r[s][a][c][i - 1] + c) % mod; } } } } while(cin >> n, n){ vector<int> d(n); rep(i, 0, n) cin >> d[i]; int sa = -1, aa = -1, ca = -1; double MIN = 1e12; rep(s, 0, 16){ rep(a, 0, 16){ rep(c, 0, 16){ double h = 0.0; vector<int> after(256, 0); rep(i, 0, n){ int tmp = (d[i] + r[s][a][c][i + 1]) % mod; after[tmp]++; } rep(i, 0, 256){ if(after[i] == 0) continue; double tmp = (double)(after[i]) / (double)(n); h -= tmp * log10(tmp); } if((h - MIN) < -1e-9){ MIN = h; sa = s; aa = a; ca = c; } } } } cout << sa << " " << aa << " " << ca << 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> using namespace std; #define eps 0.0000000001 bool eq(double a,double b){ return ( -eps < a-b && a-b < eps ); } int N; int t[256]; double calc(int S,int A,int C){ int cnt[256]={}; int x=S; for(int i=0;i<N;i++){ x=(A*x+C)%256; cnt[(t[i]+x)%256]++; } double res=0; for(int i=0;i<256;i++){ double d=(double)cnt[i]/(double)N; if(eq(d,0))continue; res-= d*log(d); } return res; } int main(){ while(1){ cin>>N; if(N==0)break; for(int i=0;i<N;i++)cin>>t[i]; int s,a,c; double ans=(1e10); for(int S=0;S<=15;S++){ for(int A=0;A<=15;A++){ for(int C=0;C<=15;C++){ double d=calc(S,A,C); if(d<ans-eps){ ans=d; 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
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <iostream> #include <cstdio> #include <sstream> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <limits> #include <map> #include <string> #include <cstring> #include <set> #include <deque> #include <bitset> #include <list> #include <cctype> #include <utility> using namespace std; typedef long long ll; typedef pair <int,int> P; typedef pair <int,P> PP; static const double EPS = 1e-8; static const int tx[] = {0,1,0,-1}; static const int ty[] = {-1,0,1,0}; int R(int i,int S,int A,int C){ if(i == 0) return S; return (A * R(i-1,S,A,C) + C) % 256; } int O(int* I,int i,int S,int A,int C){ return (I[i] + R(i,S,A,C)) % 256; } double H(int* freq,int N){ double sum = 0.0; for(int i=0;i<256;i++){ if(freq[i] == 0) continue; sum += ((double)freq[i]/(double)N)*log((double)freq[i]/(double)N); // printf("%lf\n",sum); // printf("%lf\n",((double)freq[i]/(double)N)*log((double)freq[i]/(double)N)); } return -sum; } int main(){ int freq[256]; int I[320]; int N; while(~scanf("%d",&N)){ if(N==0) break; for(int i=1;i<=N;i++){ scanf("%d",I+i); } double min_H = 100000.0; int res_S = 0; int res_A = 0; int res_C = 0; for(int S=0;S<=15;S++){ for(int A=0;A<=15;A++){ for(int C=0;C<=15;C++){ memset(freq,0,sizeof(freq)); for(int i=1;i<=N;i++){ // printf("i=%d I[i]=%d S=%d A=%d C=%d O=%d\n",i,I[i],S,A,C,O(I,i,S,A,C)); freq[O(I,i,S,A,C)]++; } double tmp_H = H(freq,N); if(min_H > tmp_H + EPS){ // printf("%lf\n",tmp_H); min_H = tmp_H; res_S = S; res_A = A; res_C = C; } } } } printf("%d %d %d\n",res_S,res_A,res_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
import java.util.*; public class Main { private void doit(){ Scanner sc = new Scanner(System.in); while(true){ int n = sc.nextInt(); if(n == 0) break; int [] data = new int[n + 1]; for(int i = 1; i <= n; i++){ data[i] = sc.nextInt(); } int m = 256; double ans = 1 << 24; int as=0, aa=0, ac=0; 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]; r[0] = s; for(int i = 1; i <= n; i++){ r[i] = (a * r[i-1] + c) % m; } int [] freq = new int[m]; for(int i = 1; i <= n; i++){ freq[(r[i] + data[i]) % m]++; } double nowh = 0; for(int i = 0; i < m; i++){ if(freq[i] == 0) continue; double value= ((double)freq[i] / n) * Math.log((double)freq[i] / n); nowh -= value; } if(ans > nowh + 1.0e-08){ ans = nowh; as = s; aa = a; ac = c; } } } } System.out.println(as + " " + aa + " " + ac); } } private void debug(Object... o) { System.out.println("debug = " + Arrays.deepToString(o)); } public static void main(String[] args) { new Main().doit(); } }
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 <string> #include <vector> #include <cmath> #include <map> using namespace std; double entropy(int N, const map<int,int> &x) { double H = 0; for (map<int,int>::const_iterator it=x.begin(); it!=x.end(); ++it) { H -= (double)it->second / N * log((double)it->second / 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) { map<int,int> x; 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 <stdio.h> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <vector> typedef long long int ll; #define BIG_NUM 2000000000 using namespace std; int N; void func(){ double minimum = (double)BIG_NUM,tmp; int ans_S,ans_A,ans_C,M=256,R[N+1],input[N+1],O[N+1],check[256]; for(int i = 1; i <= N; i++)scanf("%d",&input[i]); for(int s = 0; s <= 15; s++){ for(int a = 0; a <= 15; a++){ for(int c = 0; c <= 15; c++){ for(int i = 0; i <= 255; i++)check[i] = 0; 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; check[O[i]]++; } tmp = 0.0; for(int i = 0; i <= 255; i++){ if(check[i] == 0)continue; tmp += (double)-1*((double)check[i]/(double)N)*log((double)check[i]/(double)N); } if(minimum - tmp > 0.0000000001){ minimum = tmp; ans_S = s; ans_A = a; ans_C = c; } } } } printf("%d %d %d\n",ans_S,ans_A,ans_C); } int main(){ while(true){ scanf("%d",&N); if(N == 0)break; func(); } 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]; int[][][][] table = new int[16][16][16][256]; 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 < 256; ++i) { r = (a * r + c) & 0xFF; table[s][a][c][i] = r; } } } } 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) { OUT: for (int c = 0; c < 16; ++c) { Arrays.fill(hist, 0); for (int i = 0; i < N; ++i) { hist[(I[i] + table[s][a][c][i]) & 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) continue OUT; } } 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 <cstdio> #include <cmath> using namespace std; #define EPS 1e-10 bool isequal(double a, double b) { return fabs(a-b) < EPS; } int N,I[256]; double calc(int S, int A, int C) { int R=S, cnt[256]={0}; for(int i=0; i<N; i++) { R=(A*R+C)%256; cnt[(I[i]+R)%256]++; } double ret=0; for(int i=0; i<256; i++) { if(cnt[i]!=0) ret+=(1.0*cnt[i]/N)*log(1.0*cnt[i]/N); } return -ret; } int main() { while(scanf("%d", &N), N) { for(int i=0; i<N; i++) scanf("%d", &I[i]); int as=99, aa=99, ac=99; double mi=1e10; for(int s=0; s<=15; s++) for(int a=0; a<=15; a++) for(int c=0; c<=15; c++) { double ret=calc(s,a,c); if(isequal(ret,mi)) { if(s<as) { as=s; aa=a; ac=c; } else if(as==s&&a<aa) { aa=a; ac=c; } else if(as==s&&aa==a&&c<ac) { ac=c; } } else if(ret<mi) { mi=ret; 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<bits/stdc++.h> using namespace std; typedef long long int64; int N; vector< int > I; int S, A, C; int R(int i) { if(i == 0) return(S); return((A * R(i - 1) + C) % 256); } int O(int i) { return((I[i - 1] + R(i)) % 256); } int main() { while(cin >> N, N){ I.resize(N); for(int i = 0; i < N; i++) { cin >> I[i]; } const int INF = 1 << 30; double RH = INF; int RS, RA, RC; for(int SS = 0; SS <= 15; ++SS) { for(int AA = 0; AA <= 15; ++AA) { for(int CC = 0; CC <= 15; ++CC) { S = SS, A = AA, C = CC; vector< int > HAETA(256, 0); for(int M = 1; M <= N; M++) ++HAETA[O(M)]; double ret = 0; for(int i = 0; i < 256; i++) { if(HAETA[i] == 0) continue; ret -= ((double)HAETA[i] / N) * log((double)HAETA[i] / N); } if(ret + 1e-8 < RH) { RH = ret; RS = S; RA = A; RC = C; } } } } cout << RS << " " << RA << " " << RC << 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> using std::cin; using std::cout; using std::endl; int main(void) { int N; int M = 256; cin >> N; while ( N != 0 ) { std::vector<int> I(N); for (int i = 0; i < N; i++) { cin >> I[i]; } bool find = false; int R,O; int S,A,C; int now; double ret = 0; for (int s = 0; s < 16 && !(find); s++) { for (int a = 0; a < 16 && !(find); a++) { for (int c = 0; c < 16 && !(find); c++) { std::vector<int> counter(M, 0); R = s; for (int i = 0; i < N; i++) { R = ( (a*R) + c ) % M; O = ( I[i] + R ) % M; counter[O]++; } now = 0; for (int i = 0; i < M; i++) { if ( counter[i] != 0 ) { now += -( (double)counter[i]/N ) * std::log((double)counter[i]/N) * pow(10,5); } } if ( now == 0 ) { S = s; A = a; C = c; find = true; } else { if ( ret == 0 ) { S = s; A = a; C = c; ret = now; } else { if ( now < ret ) { S = s; A = a; C = c; ret = now; } } } } } } cout << S << " " << A << " " << C << endl; cin >> 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.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.Arrays; import java.util.InputMismatchException; import java.util.NoSuchElementException; public class Main{ static PrintWriter out; static InputReader ir; static final double EPS=1e-10; static void solve(){ for(;;){ int n=ir.nextInt(); if(n==0) break; int s=-1,a=-1,c=-1; double mi=Double.MAX_VALUE; int[] x=new int[256]; int[] I=ir.nextIntArray(n); for(int i=0;i<=15;i++){ for(int j=0;j<=15;j++){ for(int k=0;k<=15;k++){ int r=i,o; for(int l=0;l<n;l++){ r=(r*j+k)%256; o=(r+I[l])%256; x[o]++; } double res=0; for(int l=0;l<256;l++){ if(x[l]!=0){ double t=(double)x[l]/n; res-=t*Math.log(t); } } if(res+EPS<mi){ mi=res; s=i; a=j; c=k; } Arrays.fill(x, 0); } } } out.println(s+" "+a+" "+c); } } static boolean check(int x,int y,int z,int n){ return x>=0&&y>=0&&z>=0&&x<n&&y<n&&z<n; } public static void main(String[] args) throws Exception{ ir=new InputReader(System.in); out=new PrintWriter(System.out); solve(); out.flush(); } static class InputReader { private InputStream in; private byte[] buffer=new byte[1024]; private int curbuf; private int lenbuf; public InputReader(InputStream in) {this.in=in; this.curbuf=this.lenbuf=0;} public boolean hasNextByte() { if(curbuf>=lenbuf){ curbuf= 0; try{ lenbuf=in.read(buffer); }catch(IOException e) { throw new InputMismatchException(); } if(lenbuf<=0) return false; } return true; } private int readByte(){if(hasNextByte()) return buffer[curbuf++]; else return -1;} private boolean isSpaceChar(int c){return !(c>=33&&c<=126);} private void skip(){while(hasNextByte()&&isSpaceChar(buffer[curbuf])) curbuf++;} public boolean hasNext(){skip(); return hasNextByte();} public String next(){ if(!hasNext()) throw new NoSuchElementException(); StringBuilder sb=new StringBuilder(); int b=readByte(); while(!isSpaceChar(b)){ sb.appendCodePoint(b); b=readByte(); } return sb.toString(); } public int nextInt() { if(!hasNext()) throw new NoSuchElementException(); int c=readByte(); while (isSpaceChar(c)) c=readByte(); boolean minus=false; if (c=='-') { minus=true; c=readByte(); } int res=0; do{ if(c<'0'||c>'9') throw new InputMismatchException(); res=res*10+c-'0'; c=readByte(); }while(!isSpaceChar(c)); return (minus)?-res:res; } public long nextLong() { if(!hasNext()) throw new NoSuchElementException(); int c=readByte(); while (isSpaceChar(c)) c=readByte(); boolean minus=false; if (c=='-') { minus=true; c=readByte(); } long res = 0; do{ if(c<'0'||c>'9') throw new InputMismatchException(); res=res*10+c-'0'; c=readByte(); }while(!isSpaceChar(c)); return (minus)?-res:res; } public double nextDouble(){return Double.parseDouble(next());} public int[] nextIntArray(int n){ int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=nextInt(); return a; } public long[] nextLongArray(int n){ long[] a=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } public char[][] nextCharMap(int n,int m){ char[][] map=new char[n][m]; for(int i=0;i<n;i++) map[i]=next().toCharArray(); return map; } } }
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 <cmath> #include <tuple> using namespace std; typedef tuple<int,int,int> T; const int M = 256; int main(){ int N; while(cin>>N,N){ vector<int> I(N); for(auto &i:I)cin>>i; double entropy = 1e20; int occur[M]; T ans; for(int S=0;S<16;S++){ for(int A=0;A<16;A++){ for(int C=0;C<16;C++){ int R = S; fill(occur,occur+M,0); for(auto i:I){ R=(A*R+C)%M; occur[(R+i)%M]++; } double H = 0; sort(occur,occur+M); for(int i=0;i<M;i++){ if(occur[i])H-=double(occur[i])/N*log(double(occur[i])/N); } if(entropy>H){ entropy=H; ans=T(S,A,C); } } } } int S,A,C; tie(S,A,C)=ans; 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 <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <cassert> #include <iostream> #include <iomanip> #include <sstream> #include <algorithm> #include <numeric> #include <complex> #include <stack> #include <queue> #include <list> #include <set> #include <map> #include <bitset> #include <functional> #include <iterator> using namespace std; #define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl #define repi(i,a,b) for(int i=int(a);i<int(b);i++) #define peri(i,a,b) for(int i=int(b);i-->int(a);) #define rep(i,n) repi(i,0,n) #define per(i,n) peri(i,0,n) #define iter(c) __typeof__((c).begin()) #define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i) #define all(c) (c).begin(),(c).end() #define mp make_pair typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; typedef pair<int,int> pii; const int INFTY=1<<29; const double EPS=1e-9; template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){ return os<<'('<<p.first<<','<<p.second<<')'; } template<typename T> ostream& operator<<(ostream& os,const vector<T>& a){ os<<'['; rep(i,a.size()) os<<(i?" ":"")<<a[i]; return os<<']'; } const int M=256; double calc(int S,int A,int C,int* I,int N) { int R[N]; R[0]=S; repi(i,1,N+1) R[i]=(A*R[i-1]+C)%M; int hist[256]={}; repi(i,1,N+1) hist[(I[i]+R[i])%M]++; double res=0; rep(i,256) if(hist[i]) res+=-1.*hist[i]/N*log(1.*hist[i]/N); return res; } int main() { for(int N;cin>>N,N;){ int I[N+1]; rep(i,N) cin>>I[i+1]; double mn=1e100; int S0,A0,C0; rep(S,16) rep(A,16) rep(C,16){ double tmp=calc(S,A,C,I,N); if(mn-EPS>tmp) S0=S,A0=A,C0=C,mn=tmp; } cout<<S0<<' '<<A0<<' '<<C0<<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 <cmath> using namespace std; #define EPS (1e-10) #define EQ(a,b) (abs((a)-(b))<EPS) const int m=256; const int INF=1000000000; int val[300]; int calc(int idx,int a,int c){ if(val[idx]!=INF) return val[idx]; int res=(a*calc(idx-1,a,c)+c)%m; return val[idx]=res; } int main(){ int n; while(cin>>n&&n!=0){ int ap[256]; int l[300]; double minTropy=INF; int aa,cc,ss; for(int i = 0; i < n; i++) cin>>l[i]; for(int s = 0; s <= 15; s++){ for(int a = 0; a <= 15; a++){ for(int c = 0; c <= 15; c++){ fill(val,val+300,INF); fill(ap,ap+256,0); val[0]=s; if(a==7&&c==9){ // cout<<endl; } for(int i = 1; i <= n; i++){ ap[(l[i-1]+calc(i,a,c))%m]++; } double sum=0; for(int i = 0; i < 256; i++){ if(ap[i]!=0){ sum-=(1.0*ap[i]/n)*(log((double)ap[i])/n); } } if(!EQ(sum,minTropy)&&sum<minTropy){ minTropy=sum; ss=s;aa=a;cc=c; } } } } cout<<ss<<" "<<aa<<" "<<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 <bits/stdc++.h> using namespace std; #define all(c) (c).begin(),(c).end() #define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--) #define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++) #define rep(i,n) REP(i,0,n) #define iter(c) __typeof((c).begin()) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) #define mem(a) memset(a,0,sizeof(a)) #define pd(a) printf("%.10f\n",a) #define pb(a) push_back(a) #define in(a) insert(a) #define pi M_PI #define R cin>> #define F first #define S second #define C class #define ll long long #define ln cout<<'\n' #define _(_1,_2,_3,N,...)N #define pr(...) _(__VA_ARGS__,pr3,pr2,pr1)(__VA_ARGS__) template<C T>void pr1(T a){cout<<a;ln;} template<C T,C T2>void pr2(T a,T2 b){cout<<a<<' '<<b;ln;} template<C T,C T2,C T3>void pr3(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;} template<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;} bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;} const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; typedef pair<int,int> P; void Main() { int n; while(R n && n) { int x[n]; rep(i,n) R x[i]; double ans=MAX; int z[3]; rep(s,16) { rep(a,16) { rep(c,16) { vector<int> y(n+1); y[0]=s; int o[256]; mem(o); REP(i,1,n+1) { y[i]=(a*y[i-1]+c)%256; o[(y[i]+x[i-1])%256]++; } double h=0; rep(i,256) if(o[i]) h+=-o[i]*(log(o[i])-log(n)); if(h+1e-10<ans) { ans=h; z[0]=s; z[1]=a; z[2]=c; } } } } PR(z,3); } } int main() { ios::sync_with_stdio(0);cin.tie(0); Main();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.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; int[] I = new int[n]; int[] R = new int[n]; for (int i = 0; i < n; i++) I[i] = sc.nextInt(); int S = 0, A = 0, C = 0; double min = Double.MAX_VALUE; for (int s = 0; s < 16; s++) for (int a = 0; a < 16; a++) for (int c = 0; c < 16; c++) { int[] O = new int[256]; R[0] = (a * s + c) % 256; O[(I[0] + R[0]) % 256]++; for (int i = 1; i < n; i++) { R[i] = (a * R[i - 1] + c) % 256; O[(I[i] + R[i]) % 256]++; } double H = 0; for (int i = 0; i < 256; i++) if (O[i] > 0) H -= O[i] / (double) n * Math.log(O[i] / (double) n); if (H + 1e-10 < min) { min = H; S = s; A = a; C = c; } } System.out.println(S + " " + A + " " + C); } sc.close(); } }
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 <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <climits> #include <cfloat> using namespace std; const double EPS = 1.0e-10; const int MOD = 256; int main() { for(;;){ int n; cin >> n; if(n == 0) return 0; vector<int> l(n+1); for(int i=1; i<=n; ++i) cin >> l[i]; double H = DBL_MAX; 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){ vector<int> r(n+1); r[0] = s; for(int i=1; i<=n; ++i) r[i] = (a * r[i-1] + c) % MOD; vector<int> o(n+1); for(int i=1; i<=n; ++i) o[i] = (l[i] + r[i]) % MOD; vector<int> x(MOD, 0); for(int i=1; i<=n; ++i) ++ x[o[i]]; double h = 0.0; for(int i=0; i<MOD; ++i){ if(x[i] > 0) h -= x[i] / (double)n * log(x[i] / (double)n); } if(h < H - EPS){ H = 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> using namespace std; typedef pair<int,int>pint; typedef vector<int>vint; #define pb push_back #define mp make_pair #define rep(i,n) for(int i=0;i<(n);i++) template<class T,class U>void chmin(T &t,U f){if(t>f)t=f;} template<class T,class U>void chmax(T &t,U f){if(t<f)t=f;} int N; int L[256],R[256],O[256]; int S,A,C; double mi; int main(){ while(cin>>N,N){ rep(i,N)cin>>L[i]; mi=1001001001; rep(s,16)rep(a,16)rep(c,16){ memset(O,0,sizeof(O)); int latte=s; double H=0; rep(i,N){ latte=(latte*a+c)%256; R[i]=latte; O[(L[i]+R[i])%256]++; } rep(i,256)if(O[i]){ H-=O[i]*1.0/N*log(O[i]*1.0/N); } if(mi>1e-9+H){ S=s;A=a;C=c; mi=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
#define _USE_MATH_DEFINES #include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <cfloat> #include <climits> #include <cstring> #include <cmath> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<ll, int> ll_i; typedef pair<double, int> d_i; typedef pair<ll, ll> ll_ll; typedef pair<double, double> d_d; struct edge { int u, v; ll w; }; ll MOD = 1000000007; ll _MOD = 1000000009; double EPS = 1e-10; double entropy(vector<int> I, int S, int A, int C) { vector<int> a(256); int N = I.size(), R = S; for (int i = 0; i < N; i++) { R = (A * R + C) % 256; I[i] = (I[i] + R) % 256; a[I[i]]++; } double x = 0; for (int j = 0; j < 256; j++) if (a[j]) { double r = (double)a[j] / N; x -= r * log(r); } return x; } int main() { for (;;) { int N; cin >> N; if (N == 0) break; vector<int> I(N); for (int i = 0; i < N; i++) cin >> I[i]; int s, a, c; double mini = DBL_MAX; for (int S = 0; S < 16; S++) for (int A = 0; A < 16; A++) for (int C = 0; C < 16; C++) { double x = entropy(I, S, A, C); if (x < mini - EPS) { s = S; a = A; c = C; mini = x; } } 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 <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() { 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
/// // File: 2165.cpp // Author: ymiyamoto // // Created on Tue Nov 28 22:51:14 2017 // #include <cmath> #include <cstdint> #include <iostream> #include <map> #include <vector> using namespace std; #define EPS 1e-9 #define M 256 uint32_t R(uint32_t i, uint32_t S, uint32_t A, uint32_t C) { if (i == 0) return S; else return (A * R(i - 1, S, A, C) + C) % M; } double H(vector<uint32_t> O) { double h = 0.0; uint32_t N = O.size(); map<uint32_t, uint32_t> nums; for (uint32_t i = 0; i < O.size(); i++) { if (nums.count(O[i]) == 0) { nums[O[i]] = 1; } else { nums[O[i]] += 1; } } for (auto iter = nums.begin(); iter != nums.end(); iter++) { h += (double)iter->second * log((double)iter->second / (double)N); } return -h; } int32_t main() { while (true) { uint32_t N; cin >> N; if (N == 0) break; vector<uint32_t> I(N + 1, 0); for (uint32_t i = 1; i <= N; i++) { cin >> I[i]; } double min_O = 1e6; uint32_t max_S, max_A, max_C; for (uint32_t S = 0; S <= 15; S++) { for (uint32_t A = 0; A <= 15; A++) { for (uint32_t C = 0; C <= 15; C++) { vector<uint32_t> O; for (uint32_t i = 1; i <= N; i++) { O.push_back((I[i] + R(i, S, A, C)) % M); } double o = H(O); if (o + EPS < min_O) { min_O = o; max_S = S; max_A = A; max_C = C; } } } } cout << max_S << " " << max_A << " " << max_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 <set> #include <cstdio> #include <cstring> #include <cmath> #define REP(i,n) for(int i=0; i<(int)(n); i++) #define f first #define s second #define mp make_pair inline int getInt(){ int s; scanf("%d", &s); return s; } using namespace std; int l[256]; int cnt[256]; int main(){ while(int n = getInt()){ REP(i,n) l[i] = getInt(); pair<int, pair<int, int> > ans; double ansval = 1e20; REP(s,16) REP(a,16) REP(c,16){ memset(cnt, 0, sizeof(cnt)); int r = s; REP(i,n){ r = (r * a + c) % 256; cnt[(l[i] + r) % 256]++; } double h = 0.0; REP(i,256) if(cnt[i]){ double tmp = (double)cnt[i] / n; h += - tmp * log(tmp); } if(h + 1e-10 < ansval){ ans = mp(s, mp(a, c)); ansval = h; } } printf("%d %d %d\n", ans.f, ans.s.f, ans.s.s); } 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 x[256]{}; rep(i, n) { r = (a*r + c) % MOD; x[(I[i] + r) % MOD]++; } double sum = 0; rep(i, 256) { if (x[i]) { double d = x[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
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]; double[] en = new double[N + 1]; 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) { 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) { e += en[hist[i]]; if (e > best) continue OUT; } } 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
import math M = 256 while 1: N = int(raw_input()) if N == 0: break H = 1e10 I = 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 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 <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; } } } } 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 "bits/stdc++.h" using namespace std; #define PB push_back #define MP make_pair #define REP(i,n) for(int i=0;i<(n);i++) int N; int L[256],R[256],O[256]; int S,A,C; double mi; int main(){ while(cin>>N,N){ REP(i,N)cin>>L[i]; mi=1e9; REP(s,16)REP(a,16)REP(c,16){ memset(O,0,sizeof(O)); int t=s; double H=0; REP(i,N){ t=(t*a+c)%256; R[i]=t; O[(L[i]+R[i])%256]++; } REP(i,256)if(O[i]){ H-=O[i]*1.0/N*log(O[i]*1.0/N); } if(mi>1e-9+H){ S=s;A=a;C=c; mi=H; } } 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 <algorithm> using namespace std; const double eps = 1e-8; const int M = 256; 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]; } int ans[3]={0,0,0}; double minH = 1e18; for(int s=0; s<=15; s++){ for(int a=0; a<=15; a++){ for(int c=0; c<=15; c++){ vector<int> count(M,0); int r=s; for(int i=0; i<n; i++){ r = (a*r +c) %M; count[(r+l[i]) %M]++; } double H=0; for(int i=0; i<M; i++){ if(count[i]!=0){ H += -count[i]*log((double)count[i]/n); } } if(H + eps < minH){ minH = 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 <algorithm> #include <cmath> using namespace std; const int M = 256; const double EPS = 1e-9; int n, I[M]; 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]++; } double res = 0.0; for(int i=0;i<M;i++) if(cnt[i]) res -= (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]; double valm = 1e77; for(int S=0;S<=15;S++){ for(int A=0;A<=15;A++){ for(int C=0;C<=15;C++){ double tmp = calc(S, A, C); if(valm > tmp && !(abs(valm - tmp) < EPS)){ 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 <vector> #include <list> #include <map> #include <set> #include <stack> #include <queue> #include <deque> #include <algorithm> #include <utility> #include <functional> #include <sstream> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <climits> #include <cassert> using namespace std; inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;} template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();} typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define REP(i,n) FOR(i,0,(n)-1) const double EPS = 1e-10; const double PI = acos(-1.0); const int INF = INT_MAX/10; int main() { const int M = 256; int N; while(cin >> N, N) { vi I(N+1); FOR(i, 1, N) { cin >> I[i]; } vi R(N+1), O(N+1); double minH = INF; int aS, aA, aC; REP(S, 16) { REP(A, 16) { REP(C, 16) { R[0] = S; vi num(256); FOR(i, 1, N) { R[i] = (A*R[i-1] + C) % M; O[i] = (I[i]+R[i]) % M; num[O[i]]++; } double H = 0.0; REP(i, 256) { if(num[i]>0) { H -= (double)num[i]/N*log(num[i]*1.0/N); } } if(H+EPS < minH) { minH = 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 <cstdio> #include <cstdlib> #include <utility> #include <typeinfo> #include <cmath> #include <numeric> #include <algorithm> #include <iostream> #include <sstream> #include <iomanip> #include <vector> #include <stack> #include <string> #define REP(i,n) for(int i=0;i<n;i++) typedef long long ll; using namespace std; typedef vector<ll> vl; typedef pair<ll,ll> pll; typedef vector<pll> vpll; typedef vector<string> vs; int r[16][16][16][257]={}; int ii[257]={}; int xx[16][16][16][257]={}; int main() { REP(i,16)REP(j,16)REP(k,16)REP(l,257){if(l==0)r[i][j][k][l]=i;else{r[i][j][k][l]=(j*r[i][j][k][l-1]+k)%256;}} int n; while(cin>>n&&n) { REP(i,16)REP(j,16)REP(k,16)REP(l,257){xx[i][j][k][l]=0;ii[l]=0;} REP(i,n) { cin>>ii[i+1]; REP(j,16)REP(k,16)REP(l,16) { xx[j][k][l][(ii[i+1]+r[j][k][l][i+1])%256]++; } } double ans(0); int x(0),y(0),z(0); REP(i,16)REP(j,16)REP(k,16) { double tmp(0); REP(l,256) { if(xx[i][j][k][l]!=0) { tmp += xx[i][j][k][l]*log(xx[i][j][k][l]); } } if(tmp>ans) { ans = tmp; x=i; y=j; z=k; } } 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 <cstdio> #include <cmath> #define EPS 1e-9 using namespace std; int S,A,C,N,M=256; int l[257]; int u[257]; int main(){ int i,s,a,c; double b; while(1){ scanf("%d",&N); if(!N){break;} for(i=1;i<=N;i++){ scanf("%d",&l[i]); } double r=1e5; for(S=15;S>=0;S--){ for(A=15;A>=0;A--){ for(C=15;C>=0;C--){ int q,m[256]={0}; double p=0.0; u[0]=S; for(i=1;i<=N;i++){ u[i]=(A*u[i-1]+C)%M; m[(l[i]+u[i])%M]++; } for(i=0;i<256;i++){ if(m[i]){p+=((double)(m[i])/N*log((double)(m[i])/N));} } if(r+EPS>=-p){ r=-p; 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> using namespace std; #define for_(i,a,b) for(int i=a;i<b;i++) #define minit(a,b) memset(a,b,sizeof(a)) int N; int l[300]; int MOD = 256; int r[300], o[300]; void make_str(int S, int A, int C) { minit(r,0); minit(o,0); r[0] = (A*S + C) % MOD; for_(i,1,N) r[i] = (A * r[i - 1] + C) % MOD; for_(i,0,N) o[i] = (l[i] + r[i]) % MOD; } int occur[300]; double entropy() { minit(occur,0); for_(i,0,N) occur[o[i]]++; double ret = 0; for_(i,0,300) { if (occur[i] == 0) continue; double mul = (double)occur[i] / (double)N; ret -= mul * log(mul); } return ret; } void solve() { double minH = 1e9; int ansS = -1, ansA = -1, ansC = -1; for_(S,0,16) { for_(A,0,16) { for_(C,0,16) { make_str(S, A, C); double H = entropy(); if (minH > H + 1e-9) { minH = H; ansS = S; ansA = A; ansC = C; } } } } cout << ansS << " " << ansA << " " << ansC << endl; } int main() { while (cin >> N, N) { for_(i,0,N) cin >> l[i]; 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; int main() { int n; while (scanf("%d", &n) && n){ int I[257]; for (int i = 1; i <= n; i++) scanf("%d", I + i); pair<int, pair<int, int> > res = make_pair(20, make_pair(20, 20)); double mini = 1e30; for (int S = 0; S <= 15; S++){ for (int A = 0; A <= 15; A++){ for (int C = 0; C <= 15; C++){ int R[257]; int occ[256] = {0}; 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++){ occ[(I[i] + R[i]) % 256]++; } double H = 0; for (int i = 0; i < 256; i++){ if (occ[i] != 0){ double X = occ[i] * 1.0 / n; H -= X * log2(X); } } if (H - mini < -1e-10){ res = make_pair(S, make_pair(A, C)); mini = H; } } } } printf("%d %d %d\n", res.first, res.second.first, res.second.second); } 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) double eps = 1e-10; const int INF = 1e+9; typedef pair<int, int> P; typedef pair<int, P> pp; const int MAX_N = 105000; int M = 256; int N, I[512]; int main() { while(cin >> N && N){ int S, A, C; double minum = INF; REP(i, N){ cin >> I[i + 1]; } REP(s, 16)REP(a, 16)REP(c, 16){ int O[256] = {0}; int R = s; for(int i = 1; i <= N; ++i){ R = ((a * R) + c) % M; O[(I[i] + R) % M]++; } double H = 0; REP(i, 256){ if(O[i] > 0) H -= O[i] / (double)N * log(O[i] / (double)N); } if(H + eps < minum){ minum = 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 "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int,int> pii; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define all(a) (a).begin(),(a).end() #define pb emplace_back #define INF (1e9+1) double entoropy(vector<int> a, vector<int> b ){ vector<int> O(a.size()); rep(i,a.size()){ O[i] = (a[i]+b[i+1])%256; } vector<int> hist(256,0); rep(i,O.size()){ hist[O[i]]++; } int n = a.size(); double ret = 0; rep(i,256){ if(hist[i]==0)continue; ret -= (double)hist[i]/n*log((double)hist[i]/n); } return ret; } int main(){ int n; while(cin>>n&&n){ vector<int> I(n); rep(i,n)cin>>I[i]; double h = INF; int S,A,C; rep(s,15+1){ rep(a,15+1){ rep(c,15+1){ vector<int> R(n+1); R[0] = s; for(int i=1;i<=n;i++){ R[i] = (a*R[i-1]+c) % 256; } double th = entoropy(I,R); if(h-(1e-12)>th){ S = s; A = a; C = c; h = th; } } } } 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 <sstream> #include <cstdio> #include <string> #include <vector> #include <algorithm> #include <complex> #include <cstring> #include <cstdlib> #include <cmath> #include <cassert> #include <climits> #include <queue> #include <set> #include <map> #include <valarray> #include <bitset> #include <stack> using namespace std; #define REP(i,n) for(int i=0;i<(int)n;++i) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define ALL(c) (c).begin(), (c).end() typedef long long ll; typedef pair<int,int> pii; const int INF = 1<<29; const double PI = acos(-1); const double EPS = 1e-8; int main() { int n; while(cin>>n,n) { int I[n]; REP(i,n) cin>>I[i]; int s,a,c; double minH = INF; REP(S,16) REP(A,16) REP(C,16) { int R[n+1]; R[0] = S; REP(i,n) R[i+1] = (A*R[i]+C) % 256; int num[256] = {}; REP(i,n) num[(I[i]+R[i+1]) % 256]++; double H = 0; REP(i,256) { if (num[i]) { H -= (double)num[i]/n*log((double)num[i]/n); } } //cout << H << " " << S << " " << A << " " << C << endl; if (minH-EPS > H) { minH = 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 REP(i, n) for(int i = 0; i < (int) (n); i++) #define ALL(v) (v).begin(), (v).end() #define INF 1e9+7 using namespace std; typedef long double ld; ld calc(int s, int a, int c, const vector<int> &in){ int r[260]; r[0] = s; for(int i=1; i<260; i++){ r[i] = (a * r[i-1] + c) % 256; } map<int, int> count; REP(i, in.size()){ int target = (in[i] + r[i+1]) % 256; if(count.find(target)!=count.end()) count[target]++; else count[target] = 1; } ld res = 0; for(auto itr = count.begin(); itr != count.end(); itr++){ res -= itr->second * (log(itr->second) - log(in.size())); } return res; } int main(){ int n; while(cin >> n and n){ vector<int> nums(n); REP(i, n) cin >> nums[i]; ld tmp = INF; int rs, ra, rc; REP(i, 16) REP(j, 16) REP(k, 16){ ld tres = calc(i, j, k, nums); if(tmp > tres){ rs = i; ra = j; rc = k; tmp = tres; } } cout << rs << " " << ra << " " << rc << 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 <math.h> #include <map> #define MAX_SAC 15 #define EPS 0.0000000001 using namespace std; int main(int argc, char const *argv[]) { int s,a,c; int m=256; int n; int mins,mina,minc; double min; double h; while(1){ cin>>n; if(n==0) break; vector<int> l(n); for(int i1=0;i1<n;i1++){ cin>>l[i1]; } min=100000000.0; mins = 0; mina = 0; minc = 0; for(s=0;s<=MAX_SAC;s++){ for(a=0;a<=MAX_SAC;a++){ for(c=0;c<=MAX_SAC;c++){ int r=s; h = 0; map<int,int> mp; for(int i1=0;i1<n;i1++){ r=(a*r+c)%m; mp[(l[i1]+r)%m]++; } for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++){ h-=((double)(*it).second/m)*log((double)(*it).second/m); } if((min-EPS)>h) { min = h; mins = s; mina = a; minc = c; } } } } cout<<mins<<" "<<mina<<" "<<minc<<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.Scanner; //Strange String Manipulation public class Main{ void run(){ Scanner sc = new Scanner(System.in); for(;;){ int n = sc.nextInt(); if(n==0)break; double min = 1<<29; int S = 0, A = 0, C = 0; int[] I = new int[n]; for(int i=0;i<n;i++)I[i]=sc.nextInt(); 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 = new int[256]; for(int i=0;i<n;i++){ R = (a*R+c)%256; O[(I[i]+R)%256]++; } double H = 0; for(int i=0;i<256;i++)if(O[i]>0)H-=O[i]*1.0/n*Math.log(O[i]*1.0/n); if(H+1e-10<min){ min = H; S = s; A = a; C = c; } } System.out.println(S+" "+A+" "+C); } } 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<bits/stdc++.h> using namespace std; int main(){ int n; while(cin>>n,n){ int a[n],a1,a2,a3; double mi=0; for(int i=0;i<n;i++)cin>>a[i]; for(int i=0;i<16;i++) for(int j=0;j<16;j++) for(int k=0;k<16;k++){ int R=i; int b[256]={}; for(int l=0;l<n;l++){ R=(j*R+k)%256; b[(a[l]+R)%256]++; } double h=0; //cout<<h<<endl; for(int l=0;l<256;l++) if(b[l]>0) 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<iostream> #include<cmath> #include<cfloat> using namespace std; const double EPS = 0.00000001; int main(){ int n; while(cin >> n, n){ int str[n]; for(int i = 0; i < n; i++) cin >> str[i]; int x, y, z; double score = DBL_MAX; for(int s = 0; s <= 15; s++){ for(int a = 0; a <= 15; a++){ for(int c = 0; c <= 15; c++){ int r[n+1]; r[0] = s; for(int i = 1; i < n+1; i++) r[i] = (a*r[i-1]+c)%256; int ap[256] = {}; for(int i = 0; i < n; i++) ap[(str[i]+r[i+1])%256]++; double tmp = 0; for(int i = 0; i < 256; i++){ if(ap[i] == 0) continue; double ratio = (double)ap[i]/n; tmp += -ratio * log(ratio); } if(tmp+EPS < score) score = tmp, x = s, y = a, z = c; } } } 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<bits/stdc++.h> using namespace std; const double EPS=1e-9; int main(){ int n; while(cin>>n,n){ int I[333],M=256; for(int i=0;i<n;i++)cin>>I[i]; int as,aa,ac; double ah=11111; 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 cnt[333]={}; for(int i=0;i<n;i++){ R=(A*R+C)%M; int o=(I[i]+R)%M; cnt[o]++; } double h=0.0; for(int i=0;i<333;i++){ if(cnt[i]){ h-=(double)cnt[i]/(double)n*log((double)cnt[i]/(double)n); } } if(h+EPS<ah){ ah=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
import java.io.IOException; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main{ public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); while(true){ final int N = sc.nextInt(); if(N == 0){ break; } int[] input = new int[N]; for(int i = 0; i < N; i++){ input[i] = sc.nextInt(); } int[] rnd = new int[N + 1]; int[] output = new int[N]; int[] alpha = new int[256]; double min = Double.MAX_VALUE; int min_S = -1, min_A = -1, min_C = -1; for(int S = 0; S <= 15; S++){ for(int A = 0; A <= 15; A++){ for(int C = 0; C <= 15; C++){ rnd[0] = S; Arrays.fill(alpha, 0); for(int i = 1; i < N + 1; i++){ rnd[i] = (A * rnd[i - 1] + C) % 256; } for(int i = 0; i < N; i++){ output[i] = (input[i] + rnd[i + 1]) % 256; alpha[output[i]]++; } double score = 0; for(int i = 0; i < 256; i++){ if(alpha[i] == 0){ continue; } score -= (alpha[i] / (double)(N)) * Math.log(alpha[i] / (double)(N)); } if(score < min - 1e-9){ min_A = A; min_S = S; min_C = C; min = score; //System.out.println(S + " " + A + " " + C + " " + score); //System.out.println(Arrays.toString(output)); } } } } System.out.println(min_S + " " + min_A + " " + min_C); } } }
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; using ll=long long; using ld=long double; #define rep(i,N) for(int i=0;i<(int)(N);++i) #define rrep(i,N) for(int i=(int)(N)-1;i>=0;--i) #define debug(x) cout<<#x<<"="<<x<<endl; constexpr ll MOD=1000000007; constexpr ll INF=(1LL<<61)-1; template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;} template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;} template<typename T> void fail(T v){cout << v << endl;exit(0);} //template end ld eps=1e-9; void solve(){ int N; while((cin>>N,N)){ vector<int> L(N); rep(i,N)cin>>L[i]; vector<int> res(3); ld mn=1e18; rep(S,16)rep(A,16)rep(C,16){ vector<int> R(N+1); R[0]=S; for(int i=1;i<=N;i++){ R[i]=(R[i-1]*A+C)%256; } int ct[1<<8]; memset(ct,0,sizeof(ct)); rep(i,N)ct[(L[i]+R[i+1])%256]++; ld en=0; rep(j,1<<8)if(ct[j])en-=(ld)ct[j]/N*log((ld)ct[j]/N); if(en+eps<mn){ mn=en; res={S,A,C}; } } rep(i,3)if(i!=2)cout<<res[i]<<" ";else cout<<res[i]; cout<<endl; } } int main(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); 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<cstdio> #include<string> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<random> #include<map> #include<set> using namespace std; typedef long long ll; const ll MOD = 1000000007; const ll INF = (ll)1000000007 * 1000000007; const double EPS = 1e-9; typedef pair<int, int> P; typedef unsigned int ui; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i<n;i++) #define Rep(i,sta,n) for(int i=sta;i<n;i++) int main() { int n; while (cin >> n,n) { vector<int>v; int x; rep(i, n) { cin >> x; v.push_back(x); } double mi = 1000000; int out[3] = {}; rep(s, 16) { rep(a, 16) { rep(c, 16) { int memo = s; double emt = 0; vector<int> u; rep(i, n) { memo = (a*memo + c) % 256; u.push_back((memo+v[i])%256); } sort(u.begin(), u.end()); rep(i, n) { int cnt = 1; memo = u[i]; while (i + 1 < n&&u[i + 1] == memo) { cnt++; i++; } emt -= ((double)cnt / (double)n)*log((double)cnt / (double)n); } if (emt+EPS < mi) { mi = emt; out[0] = s; out[1] = a; out[2] = c; } } } } cout << out[0] << " "<<out[1] <<" " <<out[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 <string> #include <algorithm> #include <cmath> #include <math.h> #define m 256 using namespace std; int main(){ int n; while(cin>>n){ if(n==0) break; int ii[n+1],r[n+1],o[n+1]; for(int i=1;i<=n;i++){ cin>>ii[i]; } int ss=16,aa=16,cc=16; float h,mh=100; for(int s=15;s>=0;s--){ for(int a=15;a>=0;a--){ for(int c=15;c>=0;c--){ int oc[m]; for(int i=0;i<m;i++){ oc[i]=0; } h=0; r[0]=s; for(int i=1;i<=n;i++){ r[i]=(a*r[i-1]+c)%m; o[i]=(r[i]+ii[i])%m; oc[o[i]]++; } for(int i=0;i<m;i++){ if(oc[i]>0){ float h1=oc[i],h2=n; h=h-(10*h1/h2)*log(h1/h2); } } if(h<mh){ ss=s; aa=a; cc=c; mh=h; } else if(h==mh){ if(s<ss||a<aa||c<cc){ ss=s; aa=a; cc=c; } } } } } cout<<ss<<" "<<aa<<" "<<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<iostream> #include<cmath> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); ++i) int R[16][16][16][300]; int main() { rep (a, 16) rep (c, 16) rep (s, 16) { R[a][c][s][0] = s; rep (i, 256) { R[a][c][s][i + 1] = (a * R[a][c][s][i] + c) % 256; } } int n, I[300]; while (true) { cin >> n; if (n == 0) break; rep (i, n) cin >> I[i]; int alpha[300] = {}, ra, rc, rs; double hh = 0; rep (s, 16) rep (a, 16) rep (c, 16) { int alpha[300] = {}; rep (i, n) alpha[(I[i] + R[a][c][s][i + 1]) % 256]++; double h = 0; rep (i, 300) if (alpha[i] > 0) h -= alpha[i] * log(alpha[i]); if (hh > h + 1e-9) { hh = h; ra = a; rc = c; rs = s; } } cout << rs << " " << ra << " " << rc << 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 <cstring> using namespace std; int main(){ int n; while( cin >> n,n){ int I[256]; for(int i=0;i<n;i++) cin >> I[i]; double Hmin = -1; 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++){ double tmpH = 0.0; int R = S; int O[256]; memset(O, 0, sizeof(O)); for(int i=0;i<n;i++){ R = (A*R+C)%256; O[(I[i]+R)%256]++; } for(int i=0;i<256;i++){ if(O[i]!=0){ double d = (double)O[i]/n; tmpH -= (d*(log10((double)O[i])/log10(2.0)-log10((double)n)/log10(2.0))); // tmpH -= d*(log10(d)/log10(2.0)); } } if(Hmin<-0.5||tmpH<Hmin){ Hmin = tmpH; 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 <algorithm> #include <cmath> #include <vector> using namespace std; void solve(const int& n, const vector<int>& v) { double minH = 1e25; int bs, ba, bc; for (int s = 0; s <= 15; ++s) { for (int a = 0; a <= 15; ++a) { for (int c = 0; c <= 15; ++c) { int occ[256]; fill(occ, occ + 256, 0); int r = s; for(int j = 0; j < n; ++j) { r = (a * r + c) % 256; ++occ[(r + v[j]) % 256]; } double h = 0; for(int j = 0; j < 256; ++j) { if(occ[j] != 0) { h += -log((double)occ[j] / n) * occ[j] / n; } } if(h + 1e-9 < minH) { minH = h; bs = s; ba = a; bc = c; } } } } cout << bs << ' ' << ba << ' ' << bc << endl; } int main() { int n; vector<int> a; while (true) { cin >> n; if(n == 0) break; a.resize(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } solve(n, a); } 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 <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <ctime> #include <vector> #include <string> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <functional> #include <algorithm> using namespace std; #define rep(i,j) REP((i), 0, (j)) #define REP(i,j,k) for(int i=(j);(i)<(k);++i) #define BW(a,x,b) ((a)<=(x)&&(x)<=(b)) #define ALL(v) (v).begin(), (v).end() #define LENGTHOF(x) (sizeof(x) / sizeof(*(x))) #define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b) #define SQ(x) ((x)*(x)) #define Mod(x, mod) (((x)+(mod)%(mod)) #define MP make_pair #define PB push_back #define Fi first #define Se second #define INF (1<<29) #define EPS 1e-10 #define MOD 1000000007 typedef pair<int, int> pi; typedef pair<int, pi> pii; typedef vector<int> vi; typedef queue<int> qi; typedef long long ll; int N; int I[256+1], R[256+1], O[256+1]; const int M = 256; int main() { while(1){ cin >> N; if(N == 0) break; rep(i, N) cin >> I[i]; double H = INF; int S = 0, A = 0, C = 0; rep(s, 15+1) rep(a, 15+1) rep(c, 15+1){ memset(O, 0, sizeof(O)); R[0] = s; for(int i=1;i<=N;i++) R[i] = (a*R[i-1] + c) % M; rep(i, N) O[(I[i]+R[i+1])%M]++; double h = 0; rep(i, 256){ if(O[i] == 0) continue; h += -O[i] * log((double)O[i]/(double)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 <algorithm> #include <cmath> #include <set> using namespace std; const int M = 256; int main() { while(1){ int N; cin >> N; if(N == 0) break; int I[N+1]; for(int i = 1; i <= N; i++) cin >> I[i]; int R[N+1], O[N+1], S, A, C; double H = 1.0; set<int> s; for(int i = 0; i <= 15; i++){ R[0] = i; for(int j = 0; j <= 15; j++){ for(int k = 0; k <= 15; k++){ s.clear(); for(int l = 1; l <= N; l++){ R[l] = (j*R[l-1]+k) % M; O[l] = (I[l]+R[l]) % M; s.insert(O[l]); } double h = 0.0; for(set<int>::iterator ite = s.begin(); ite != s.end(); ite++){ double a = (double)count(O+1,O+N+1,*ite); h -= a*log(a); } h /= (double)N; if(H > h){ H = h; 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
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 - 1e-10) continue OUT; } } 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<cmath> using namespace std; int main() { int N,I[256],R[256],A,S,C,a,s,c,M=256,r; double H,h,eps=1e-8; while(1){ cin>>N; if(N==0)break; for(int i=0;i<N;i++)cin>>I[i]; H=10000000.0; for(a=0;a<16;a++){ for(s=0;s<16;s++){ for(c=0;c<16;c++){ for(int i=0;i<M;i++)R[i]=0; r=s; for(int i=0;i<N;i++){ r=(a*r+c)%M; R[(I[i]+r)%M]++; } h=0.0; for(int i=0;i<M;i++){ if(R[i]!=0){ h-=(double)R[i]/N*log((double)R[i]/N); } } if(h+eps<H){ A=a; S=s; 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 <iostream> #include <string> #include <algorithm> #include <cmath> #include <math.h> #define m 256 using namespace std; int main(){ int n; while(cin>>n){ if(n==0) break; int ii[n+1],r[n+1],o[n+1]; for(int i=1;i<=n;i++){ cin>>ii[i]; } int ss=16,aa=16,cc=16; long long h,mh=10000000000000; for(int s=15;s>=0;s--){ for(int a=15;a>=0;a--){ for(int c=15;c>=0;c--){ int oc[m]; for(int i=0;i<m;i++){ oc[i]=0; } h=0; r[0]=s; for(int i=1;i<=n;i++){ r[i]=(a*r[i-1]+c)%m; o[i]=(r[i]+ii[i])%m; oc[o[i]]++; } for(int i=0;i<m;i++){ if(oc[i]>0){ float h1=oc[i],h2=n; h=h-(h1/h2)*log(h1/h2)*100000; } } if(h<mh){ ss=s; aa=a; cc=c; mh=h; } else if(h==mh){ if(s<ss||a<aa||c<cc){ ss=s; aa=a; cc=c; } } } } } cout<<ss<<" "<<aa<<" "<<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 <stdio.h> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <vector> typedef long long int ll; #define BIG_NUM 2000000000 using namespace std; int N; void func(){ double minimum = (double)BIG_NUM,tmp,bunbo=(double)N; int ans_S,ans_A,ans_C,M=256,R[N+1],input[N+1],O[N+1],check[256]; for(int i = 1; i <= N; i++)scanf("%d",&input[i]); for(int s = 0; s <= 15; s++){ for(int a = 0; a <= 15; a++){ for(int c = 0; c <= 15; c++){ for(int i = 0; i <= 255; i++)check[i] = 0; 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; check[O[i]]++; } tmp = 0.0; for(int i = 0; i <= 255; i++){ if(check[i] == 0)continue; tmp -= ((double)check[i]/bunbo)*log((double)check[i]/bunbo); } if(minimum - tmp > 0.0000000001){ minimum = tmp; ans_S = s; ans_A = a; ans_C = c; } } } } printf("%d %d %d\n",ans_S,ans_A,ans_C); } int main(){ while(true){ scanf("%d",&N); if(N == 0)break; func(); } 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.Scanner; public class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { 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) { 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) { double v = 1.0 * hist[i] / N; if (v != 0) e -= v * Math.log(v); } if (e < best - 1e-10) { best = e; bs = s; ba = a; bc = c; } } } } System.out.println(bs + " " + ba + " " + bc); } } }
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.Scanner; public class Main { /** * @param args */ public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(true){ int N = sc.nextInt(); if(N==0)break; int[] a = new int[N+1]; for (int i = 0; i < N; i++) { a[i+1]=sc.nextInt(); } double min = 1<<29; double e = 1e-10; int x=0,y=0,z=0; for (int i = 0; i <= 15; i++) { for (int j = 0; j <= 15; j++) { for (int k = 0; k <= 15 ; k++) { double H = calc(a,i,j,k); if(min > H+e){ min = H; x=i; y=j; z=k; } } } } System.out.println(x+" "+y+" "+z); } } static int m = 256; static double calc(int[] I,int s,int a,int c){ double res = 0.0; int[] o = new int[257]; int[] r = new int[I.length]; r[0]=s; for (int j = 1; j < r.length; j++) { r[j] = (a*r[j-1]+c)%m; } for (int j = 1; j < I.length; j++) { o[(I[j]+r[j])%m]++; } for (int j = 0; j < o.length; j++) { if(o[j]!=0){ double d = (double)o[j]/(I.length-1)*Math.log((double)o[j]/(I.length-1)); res -= d; } } return res; } }
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
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2165 #include<iostream> #include<map> #include<vector> #include<algorithm> #include<cmath> #include<climits> #include<ctime> #include<cstring> #include<numeric> #define ALL(v) (v).begin(),(v).end() #define REP(i,p,n) for(int i=p;i<(int)(n);++i) #define rep(i,n) REP(i,0,n) #define dump(a) (cerr << #a << "=" << (a) << endl) #define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl; using namespace std; #define EPS 1e-10 double entropy(int s, int a, int c, vector<int> I) { const int mod = 256; int n = I.size(); //R(0)を除いて、n個必要である vector<int> R(n+1); R[0] = s; rep(i,n){ R[i+1] = (a*R[i]+c) % mod; } //R(1)からn個回して、O(n)を作成する vector<int> O(n); rep(i,n){ O[i] = (I[i] + R[i+1]) % mod; } //数字の出現回数を記録する vector<int> freq(256,0); rep(i,n){ freq[O[i]]++; } //エントロピーを求める double h=0; rep(i,256) { if(!freq[i]){ continue; } h -= ((double)freq[i]/n) * log((double)freq[i]/n); } return h; } int main() { int N; while(cin >> N && N) { vector<int> I(N); rep(i,N){ cin >> I[i]; } double ans_h = 1e10; //大きい数で初期化 int ans_s, ans_a, ans_c; rep(s,16){ rep(a,16){ rep(c,16) { double h = entropy(s,a,c,I); if(h+EPS<ans_h) //誤差予防にEPSが必要 { ans_h = 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
#include<iostream> #include<sstream> #include<algorithm> #include<set> #include<map> #include<queue> #include<complex> #include<cstdio> #include<cstdlib> #include<cstring> #include<cassert> #define rep(i,n) for(int i=0;i<(int)n;i++) #define all(c) (c).begin(),(c).end() #define mp make_pair #define pb push_back #define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++) #define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pi; const int inf = (int)1e9; const double INF = 1e12, EPS = 1e-9; double calc(const vi &v){ int n = v.size(), cnt[256] = {}; double res = 0; rep(i, n) cnt[v[i]]++; rep(i, 256) if(cnt[i]){ double t = cnt[i] * 1.0 / n; res -= t * log(t) / log(2); } return res; } int main(){ int n, as, aa, ac; while(cin >> n, n){ vi t(n); double e = INF; rep(i, n) cin >> t[i]; rep(s, 16) rep(a, 16) rep(c, 16){ vi v; int k = s; rep(i, n){ k = (k * a + c) % 256; v.pb(((int)t[i] + k) % 256); } double te = calc(v); if(te + EPS < e){ e = te; 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 <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] / n); } H = -H; if(H + 1e-9 < 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
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]; 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) { 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) { 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 <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; } if( bH < 0.000000001 ) break; } } } 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<bits/stdc++.h> using namespace std; int main(){ int n; const int M=256; while(cin>>n,n){ vector<int> l(n); for(int i=0;i<n;i++){ cin>>l[i]; } double res=1e9; int ress=-1; int resa=-1; int resc=-1; for(int s=0;s<=15;s++){ for(int a=0;a<=15;a++){ for(int c=0;c<=15;c++){ vector<int> r(n); int pre=s; for(int i=0;i<n;i++){ r[i]=(a*pre+c)%M; pre=r[i]; } vector<int> o(n); for(int i=0;i<n;i++) o[i]=(l[i]+r[i])%M; vector<int> x(256,0); for(int i=0;i<n;i++) x[o[i]]++; double h=0; for(int i=0;i<256;i++){ if(x[i]!=0){ h+=(-1.0*x[i]/n*log(1.0*x[i]/n)); } } const double EPS=1e-9; if(h<res-EPS){ res=h; ress=s; resa=a; resc=c; } } } } cout<<ress<<" "<<resa<<" "<<resc<<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 <cstring> using namespace std; int main(){ int n; while(cin >> n, n){ int I[256]; for(int i=0;i<n;i++) cin >> I[i]; double Hmin = -1; 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++){ double tmpH = 0.0; int R = S; int O[256]; memset(O, 0, sizeof(O)); for(int i=0;i<n;i++){ R = (A*R+C)%256; O[(I[i]+R)%256]++; } for(int i=0;i<256;i++){ if(O[i]!=0){ double d = (double)O[i]/n; tmpH -= d*log(d); } } if(Hmin<-0.5||tmpH+1e-8<Hmin){ Hmin = tmpH; 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 <bits/stdc++.h> using namespace std; using ld = long double; const int mod = 0xff; const ld eps = 1e-10; int main() { int N; while (cin >> N, N) { vector<int> I(N); for (int i = 0; i < N; i++) { cin >> I[i]; } int rS = -1, rA = -1, rC = -1; ld mH = 1e20; 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), O(N + 1), cnt(256); R[0] = S; for (int i = 1; i <= N; i++) { R[i] = (A * R[i - 1] + C) & mod; } for (int i = 1; i <= N; i++) { O[i] = (I[i - 1] + R[i]) & mod; cnt[O[i]]++; } ld H = 0; for (int i = 0; i < 256; i++) { if (cnt[i]) { ld tmp = (ld)cnt[i] / N; H -= tmp * logl(tmp); } } if (H + eps < mH) { rS = S; rA = A; rC = C; mH = H; } } } } cout << rS << ' ' << rA << ' ' << rC << 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<queue> #include<set> #include<vector> #include<string> #include<algorithm> #include<sstream> #include<cmath> #include<stack> #include<map> #include<cstdio> using namespace std; #define rep(i,a) for(int i=0;i<a;i++) #define mp make_pair #define pb push_back #define P pair<int,int> #define ll __int64 int n; int t[500]; int M=256; int r[500],o[500]; double entro(int s,int a,int c){ rep(i,500)r[i]=o[i]=0; 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[(t[i-1]+r[i])%M]++; double h=0; for(int i=0;i<M;i++){ if(o[i]==0)continue; double sa=(double)o[i]/(double)n; h-=sa*(log(sa)); } return h; } int main(){ while(cin>>n,n){ double ma=1000000; int as=0,aa=0,ac=0; rep(i,500)t[i]=0; rep(i,n)cin>>t[i]; rep(s,16){ rep(a,16){ rep(c,16){ if(entro(s,a,c)+0.00000001<ma){ ma=entro(s,a,c); //cout<<s<<" "<<a<<" "<<c<<entro(s,a,c)<<endl; as=s; aa=a; ac=c; } } } } //cout<<entro(8,7,14)<<" "<<entro(1,7,12)<<endl; 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 <set> #include <cmath> using namespace std; const int m=256; int main() { int n,s,a,c,I[256],anss,ansa,ansc; for(;;){ cin>>n; if(!n) break; for(int i=1;i<=n;i++) cin>>I[i]; int max=n; double min=100000.0; for(s=0;s<=15;s++) for(a=0;a<=15;a++) for(c=0;c<=15;c++){ int o,r=s,cou=0,x[256]={0}; for(int i=1;i<=n;i++){ r=(a*r+c)%m; o=(I[i]+r)%m; cou+=x[o]==0; if(max<cou) break; x[o]++; } if(cou<=max){ max=cou; double h=0; for(int i=0;i<m;i++) if(x[i]) h-=(double)x[i]/n*log((double)x[i]/n); if(h+1e-9<min){ min=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 <cmath> #include <cstdio> #include <vector> using namespace std; int main(){ int N, M = 256; while(cin >> N, N){ int l[N], s = -1, a = -1, c = -1; long double H = (1e10); 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){ vector<int> V(M,0); int R = S; for(int i = 0; i < N; ++i){ R = (A*R+C)%M; ++V[(l[i]+R)%M]; } double t = 0.0; for(int i = 0; i < M; ++i) if(V[i]) t -= (double)V[i]/N*log((double)V[i]); if(t < H){ s = S; a = A; c = C; H = t; } } } } 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<cmath> #include<vector> #include<algorithm> #include<cstring> using namespace std; const double EPS = 1e-9; struct Data{int S,A,C;}; vector<int> I; int N; void input(){ I.resize(N+1); for(int i = 1; i <= N; i++) cin >> I[i]; } double calc(int S, int A, int C){ vector<int> R(N+1),O(N+1); 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] = (I[i]+R[i])%256; int app[256]; memset(app,0,sizeof(app)); for(int i = 1; i <= N; i++) app[O[i]]++; double res = 0; for(int i = 0; i < 256; i++) if(app[i] > 0) res += (double)app[i]*(log((double)app[i])-log((double)N)); return -res; } void solve(){ double H = -1; Data ans; for(int i = 0; i < 16; i++) for(int j = 0; j < 16; j++) for(int k = 0; k < 16; k++){ double tmp = calc(i,j,k); if(fabs(tmp-H) < EPS) continue; if(H == -1 || tmp < H){ H = tmp; ans = (Data){i,j,k}; } } cout << ans.S << " " << ans.A << " " << ans.C << endl; } int main(){ while(cin >> N && N){ input(); 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 LOG(...) fprintf(stderr,__VA_ARGS__) //#define LOG(...) #define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i) #define RFOR(i,a,b) for(int i=(int)(b-1);i>=(int)(a);--i) #define REP(i,n) for(int i=0;i<(int)(n);++i) #define RREP(i,n) for(int i=(int)(n-1);i>=0;--i) #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() #define EXIST(s,e) ((s).find(e)!=(s).end()) #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define SZ(a) ((int)(a).size()) #define BIT(x, i) (((x) >> (i)) & 1) #define SQ(x) ((x)*(x)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef vector<char> vc; typedef vector<vc> vvc; typedef vector<string> vs; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define INF 1000000000 int main() { int N; while (cin >> N, N) { vi in(N); REP(i, N) cin >> in[i]; double ans = INF; tuple<int, int, int> res = make_tuple(INF, INF, INF); RREP(S, 16) RREP(A, 16) RREP(C, 16) { vi out(N); map<int, int> occurences; int R = S; REP(i, N) { R = (A * R + C) % 256; out[i] = (in[i] + R) % 256; occurences[out[i]]++; } double H = 0; for (auto kv : occurences) { double r = kv.second / double(N); H += r * (log(r) / log(2)); } if (ans >= -H) { ans = -H; res = min(res, make_tuple(S, A, C)); } } printf("%d %d %d\n", get<0>(res), get<1>(res), get<2>(res)); } }
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<algorithm> #include<map> #include<set> #include<utility> #include<cmath> #include<cstring> #include<queue> #include<cstdio> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) #define pb push_back #define mp make_pair #define all(in) in.begin(),in.end() const double PI=acos(-1); const double EPS=1e-10; const int inf=1e9; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pii; int main(){ int n; while(cin>>n,n){ vi l(n); rep(i,n)cin>>l[i]; int A,C,S; double mi=1e8; rep(s,16)rep(a,16)rep(c,16){ vi R(n+1); R[0]=s; loop(i,1,n+1)R[i]=(a*R[i-1]+c)%256; vi X(260,0); rep(i,n)X[(R[i+1]+l[i])%256]++; double sum=0; rep(i,260)if(X[i])sum-=(double)X[i]/n*log((double)X[i]/n); if(mi>1e-9+sum){ mi=sum; A=a;C=c;S=s; } } 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<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int MAX_N = 256; const int MAX_SAC = 15; const double DINF = 1e70; /* typedef */ typedef unsigned char uchar; /* global variables */ int n; uchar is[MAX_N]; int cnts[256]; double xs[MAX_N]; /* subroutines */ double entropy(int s, int a, int c) { uchar r = a * s + c; memset(cnts, 0, sizeof(cnts)); for (int i = 0; i < n; i++) { uchar oi = is[i] + r; cnts[oi]++; r = a * r + c; } int k = 0; for (int i = 0; i < 256; i++) if (cnts[i] > 0) { double d = (double)cnts[i] / n; xs[k++] = -d * log(d); } sort(xs, xs + k); double sum = 0.0; for (int i = 0; i < k; i++) sum += xs[i]; return sum; } /* main */ int main() { for (;;) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { int ii; cin >> ii; is[i] = ii; } double min_e = DINF; int min_s, min_a, min_c; for (int s = 0; s <= MAX_SAC; s++) for (int a = 0; a <= MAX_SAC; a++) for (int c = 0; c <= MAX_SAC; c++) { double e = entropy(s, a, c); //printf("entropy(%d,%d,%d)=%.16lf\n", s, a, c, e); if (min_e > e) { min_e = e; min_s = s, min_a = a, min_c = c; } } printf("%d %d %d\n", min_s, min_a, min_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> using namespace std; inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;} template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();} template<class T> inline T sqr(T x) {return x*x;} typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(), (a).rend() #define pb push_back #define mp make_pair #define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) #define exist(s,e) ((s).find(e)!=(s).end()) #define range(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) range(i,0,n) #define clr(a,b) memset((a), (b) ,sizeof(a)) #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; const double eps = 1e-10; const double pi = acos(-1.0); const ll INF =1LL << 62; const int inf =1 << 29; int n; const int m=256; int l[257]; int r[257]; int num[257]; long double entropy(int s,int a,int c){ r[0]=s; range(i,1,n+1) r[i]=(a*r[i-1]+c)%m; rep(i,m) num[i]=0; range(i,1,n+1) num[(l[i]+r[i])%m]++; long double sum=0.0; rep(i,m){ long double cur=1.0*num[i]/n; if(cur==0.0) continue; sum-=(cur*log(cur)); } return sum; } int main(void){ while(cin >>n){ if(n==0) break; range(i,1,n+1) cin >> l[i]; long double cmin=entropy(0,0,0); int ss=0,aa=0,cc=0; rep(s,16)rep(a,16)rep(c,16){ double cur=entropy(s,a,c); if(cur<cmin){ cmin=cur; ss=s,aa=a,cc=c; } } cout << ss << " " << aa << " " << 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 <bits/stdc++.h> using namespace std; #define reep(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) reep((i),0,(n)) int main(){ int n; const int mod=256; while(cin>>n,n){ vector<int> v(n); rep(i,n){ cin>>v[i]; } vector<int> R(n+1); vector<int> O(256,0); long double ans=1e100; int s,a,c; rep(S,16){ rep(A,16){ rep(C,16){ R[0]=S; reep(i,1,n+1){ R[i]=(A*R[i-1]+C)%mod; } rep(i,256) O[i]=0; rep(i,n){ O[(v[i]+R[i+1])%mod]++; } long double tmp=0; rep(i,256){ if(O[i]==0) continue; double t=(double)O[i]/n; tmp-=t*log(t); } if(ans>tmp){ s=S; a=A; c=C; // cout<<s<<" "<<a<<" "<<c<<" "<<tmp<<endl; ans=tmp; } } } } 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> using namespace std; typedef long long int ll; vector<ll> a; ll n; ll mod=256; const double EPS=1e-9; double str(ll s,ll A,int c){ vector<ll> k(n+1); vector<ll> C(256,0); k[0]=s; //C[(s+a[0])%mod]++; for(int i=1;i<=n;i++){ k[i]=(k[i-1]*A+c)%mod; C[(int)((k[i]+a[i-1])%mod)]++; } double ret=0; for(int i=0;i<256;i++){ if(C[i]==0){continue;} double k=C[i]; k/=n; //cout<<k<<" "<<log(k)<<endl; k*=log(k); //cout<<k<<endl; ret+=k; } return ret; } int main(){ while(cin>>n && n){ a.clear(); a.resize(n); for(int i=0;i<n;i++){ cin>>a[i]; } double mx=str(0,0,0); ll s=0,a=0,c=0; for(int i=0;i<16;i++){ for(int t=0;t<16;t++){ for(int h=0;h<16;h++){ double ret=str(i,t,h); if(ret>mx+EPS){ mx=ret; s=i; a=t; c=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<iostream> #include<algorithm> #include<cmath> #include<iomanip> using namespace std; int n; int L[300],R[300]; double cnt[300]; int main() { while(cin>>n,n) { for(int i=0;i<n;i++)cin>>L[i]; int ansS,ansA,ansC; double m=1e9; for(int S=0;S<=15;S++) { for(int A=0;A<=15;A++) { for(int C=0;C<=15;C++) { double now=0; for(int i=0;i<n;i++)R[i]=(A*(i?R[i-1]:S)+C)%256; for(int i=0;i<300;i++)cnt[i]=0; for(int i=0;i<n;i++)cnt[(L[i]+R[i])%256]+=1; for(int i=0;i<300;i++) { if(cnt[i]<1)continue; now-=cnt[i]/n*logl(cnt[i]/n); } if((long long)(m*1e9)>(long long)(now*1e9)) { m=now; 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<cmath> using namespace std; int n, I[1 << 10], R[1 << 10], V[1 << 10]; int main() { while (true) { cin >> n; if (n == 0)break; for (int i = 1; i <= n; i++) { cin >> I[i]; } long double minx = 100.0; int T = 0; 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.0; for (int i = 1; i <= n; i++) R[i] = (A*R[i - 1] + C) % 256; for (int i = 0; i < 256; i++) V[i] = 0; for (int i = 1; i <= n; i++) V[(I[i] + R[i]) % 256]++; for (int i = 0; i < 256;i++){ if (V[i] >= 1) { long double F = 1.0*V[i] / n; sum -= F*log(F); } } if (sum < minx) { minx = sum; T = S * 256 + A * 16 + C; } } } } cout << T / 256 << ' ' << (T / 16) % 16 << ' ' << T % 16 << endl; } return 0; }
CPP