-
Notifications
You must be signed in to change notification settings - Fork 2
/
bnsonstr.cpp
93 lines (88 loc) · 1.85 KB
/
bnsonstr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
lli t,z;
cin>>t;
for(z=1;z<=t;z++)
{
long long int n,totalzero=0,totalone=0;
cin>>n;
char a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]=='0')
{
totalzero=totalzero+1;
}
else
{
totalone=totalone+1;
}
}
if(totalone==0)
{
cout<<"1"<<endl;
}
else
{
vector<int> factors;
vector<string> possible_string(n);
vector<int> swaps_done;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
factors.push_back(i);
}
}
for(int i=0;i<factors.size();i++)
{
for(int j=0;j<factors[i];j++)
{
string s="";
for(int k=0;k<factors[i];k++)
{
if(k==j)
{
s=s+'1';
}
else
{
s=s+'0';
}
}
string ss="";
for(int p=1;p<=n/factors[i];p++)
{
ss=ss+s;
}
possible_string.push_back(ss);
}
}
int ans=INT_MAX;
for(int i=0;i<possible_string.size();i++)
{
int a=0;
for(int j=0;j<n;j++)
{
if(a[j]!=(possible_string[i])[j])
{
a=a+1;
}
}
ans=min(a,ans);
}
cout<<ans<<endl;
}
}
return 0;
}