题解:P17414 「IXOI R3」贴吧 82 号
Dwl20121022 · · 题解
思路
考虑贪心。
贪心策略:从左往右扫描,若
正确性证明:
tips:
n 表示长度
- 当
n = 1 \text{,} 2 \text{,} 3 时,结论显然正确;- 当
n > 3 时,假设我们已经知道了当n = k 时的最少操作次数f_k 。
情况 1:如果
情况 2:如果
考虑反证法:
假设
所以
假设
AC code:
#include <iostream>
using namespace std;
int len, ans;
string s;
int main() {
cin >> len >> s;
s = " " + s;
for (int i = 1; i <= len; i++) {
if (s[i] == '0') {
ans++;
for (int j = 1; j * i <= len; j++) s[j * i] = (s[j * i] == '0' ? '1' : '0');
}
}
cout << ans;
return 0;
}