题解分享
题解分享简介
01 串的熵(结果填空) - 题解
暴力枚举
```
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
int n = 23333333;
for(int i=1;i<n;i++)
{
double a = i *1.0/n;
double b = (n-i)*1.0/n;
double res =0;
res-=a*log2(a)*i+b*log2(b)*(n-i);
if(fabs(res-11625907.5798)<0.0001)
{
printf("%d\n",i);
break;
}
}
return 0;
}
```
查看全文
0
0
3
2
01 串的熵(结果填空) - 题解
该死的公式可以化简为
$$
\frac{x^2}{n}log_2\frac{x}{n} + \frac{(n - x)^2}{n}log_2\frac{n-x}{n}
$$
其中 x 是 0 或者 1 在字符串中出现的次数, n - x 则是 1 或者 0 在字符串出现的次数, n 是字符串长度
然后枚举可能的出现次数即可, 注意控制精度🎉️
```cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n = 23333333;
int zf = n / 2;
double s = 11625907.5798;
// 遍历所有可能的 0 的数量
for (int i = 0; i < zf; ++i) {
double x = n - i;
double y = i;
double jg = -((x*x)/n * log2(x/n) + ((y*y)/n * log2(y/n)));
if (jg > s) {
cout << jg << " ~ " << s << '\n';
if (s - jg < 1e-6)
cout << i << '\n';
break;
}
}
return 0;
}
```
查看全文
0
0
2
1
01 串的熵(结果填空) - 题解
```
#include<iostream>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
#define N 23333333
int main()
{
for(int x=1;x<N/2;x++)
{
double p0=x*1.0/N,p1=(N-x)*1.0/N;
if(abs(-x*p0*log2(p0)-(N-x)*p1*log2(p1)-11625907.5798)<0.0001)
{
cout<<x<<endl;
return 0;
}
}
}
```
查看全文
0
0
0
7
01 串的熵(结果填空) - 题解
本题主要是浮点数的比较一个需要知道的是fabs和abs区别,另一个是知道熵是什么,然后直接暴力就行,记住不要犯常规错误(如1e6)。
include
//typedef long long ll;
using namespace std;
int main()
{
```
int n=23333333;
double check=11625907.5798;
for(int z=1;z<=n;z++)
{
int one=n-z;
if(z>one)break;
double PZ=1.0\*z/n;
double POne=1.0\*one/n;
double HZ=-z\*PZ\*log2(PZ);
double HOne=-one\*POne\*log2(POne);
double HS=HZ+HOne;
if(fabs(HS-check)<1e-4)
{
cout<<z;
break;
}
}
```
}
查看全文
0
0
0
2
01 串的熵(结果填空) - 题解
```
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
int n = 23333333 / 2;
int m = n + 1;
int q = 23333333;
int i;
for(i = n; i >= 0; i -- )
{
double a0 = -1 * 1.0 * i * (1.0 * i / q) * log2(1.0 * i / q);
double a1 = -1 * 1.0 * (q - i) * (1.0 * (q - i) / q) * log2(1.0 * (q - i) / q);
double aa = a0 + a1;
if(fabs(aa - 11625907.5798) < 0.0001)
break;
}
cout << i;
}
```
查看全文
0
0
0
2
01 串的熵(结果填空) - 题解
```
#include<bits/stdc++.h>
using namespace std;
const int N = 23333333;
double num = 11625907.5798;
double E = 1e-4;
int ans;
int main()
{
int l = 1, r = N / 2;
while (l <= r)
{
int mid = (l + r) >> 1;
int y = N - mid;
double s = -1.0 * y * y / N * log2(1.0 * y / N) - 1.0 * mid * mid / N * log2(1.0 * mid / N);
if (abs(s - num) <= E)
{
cout << mid;
break;
}
else
{
if (s > num) r--;
else if (s < num) l++;
}
}
return 0;
}
```
查看全文
0
0
0
1
01 串的熵(结果填空) - 题解
```
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
double res=11625907.5798;
int sumNumber=23333333;
double cul(int count0,int count1){
double bite0=count0*1.0/sumNumber,bite1=count1*1.0/sumNumber;
double hs=-count0*(bite0*log2(bite0))-count1*(bite1*log2(bite1));
return hs;
}
int main(){
for(int i=1;i<=sumNumber/2;i++){
int count0=i,count1=sumNumber-i;
if(abs(cul(count0,count1)-res)<1e-4){
cout<<count0;
break;
}
}
return 0;
}
```
查看全文
0
0
0
1
01 串的熵(结果填空) - 题解
```
#include <bits/stdc++.h>
using namespace std;
const double N = 23333333;
const string ans = "11625907.5798";
int main(void) {
int t = 0;
for (t = 0; t <= N; t += 1) {
double p_0 = t / N;
double p_1 = (N - t) / N;
double tmp = ((t * -p_0 * log2(p_0)) + ((N - t) * -p_1 * log2(p_1)));
char fin[ans.size()];
sprintf(fin, "%.4f", tmp);
if (ans == fin) {
cout << t;
break;
}
}
return 0;
}
```
查看全文
0
0
0
1
01 串的熵(结果填空) - 题解
```
#include<bits/stdc++.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long double db;
const int N = 23333333;
const db ans = 11625907.5798, eps = 1e-4;
int main()
{
ios::sync_with_stdio(0);cin.tie(0);
for(int v = 0;v <= N / 2;++v)
{
int u = N - v;
db res = -1.0 * u * u / N * log2(1.0 * u / N) - 1.0 * v * v / N * log2(1.0 * v / N);
if(fabs(res - ans) < eps)
{
cout<<v<<endl;
return 0;
}
}
return 0;
}
```
查看全文
0
0
0
0



