C. Removing Smallest Multiples
思路好想注意细节
4
0000
4
0010
看第二个样例
1 2 4 的时候发现即使2已经被删除了它也可以继续往下删除
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
#define int long long
const int N = 1e6+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a*b/gcd(a,b);}
int qmi(int a,int b,int mod){int res=1;while(b){if(b&1)res=res*a%mod;b>>=1;a=a*a%mod;}return res;}
int n,q,m;
// int e[N],ne[N],w[N],h[N],idx;
// void add(int a,int b,int c){
// e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx++;
// }
bool vis[N];
char s[N];
void solve()
{
cin>>n;
for(int i=1;i<=n;i++)vis[i] = 0;
for(int i=1;i<=n;i++){
char c;cin>>c;s[i] = c;
if(c=='1')vis[i] = 1;
}
int res = 0;
for(int i=1;i<=n;i++){
if(!vis[i])res+=i;
int j=i*2;
if(s[i]=='1')continue;
while(s[j]=='0'&&j<=n){
if(!vis[j]){
vis[j] = 1;
res+=i;
}
j+=i;
}
}
cout<<res<<"\n";
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _;
cin>>_;
//_ = 1;
while(_--)solve();
return 0;
}