WA on 2求调,玄关
查看原帖
WA on 2求调,玄关
1134329
long_ting楼主2024/3/7 16:37
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> Pii;


//#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
/*#define int __int128
inline __int128 read(){
    __int128 x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
inline void print(__int128 x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9) print(x / 10);
    putchar(x%10+'0');
}
*/
/*
int read(){
	int res=0;
	char c=getchar();
	while(c>'9'||c<'0') c=getchar();
	while(c<='9'&&c>='0') {res=(res<<1)+(res<<3)+(c^48);c=getchar();}
	return res;
}*/
#define int long long
//const int mod=1e9+7;

int p[500005],b[500005],ans[500005],a[500005],dp[500005],c[500005];
int tot=0;
void sieve(){
    p[1]=1;
    for(int i=2;i<=500000;i++){
        for(int j=2*i;j<=500000;j+=i)
            p[j]=1;
    }
    for(int i=1;i<=500000;i++){
        if(p[i]==0) b[++tot]=i;
    }
}
void solve(){
    int n,l;
    int cnt=0;
    cin>>n>>l;
    char x;
    for(int i=1;i<=n;i++){
        cin>>x;
        if(x=='1'){
            a[++cnt]=i;
        }
        dp[i]=0;
        ans[i]=0;
    }
    dp[0]=1;
    if(l==0){
        cout<<-1<<"\n";
        return;
    }
    int totb=0;
    for(int i=1;i<=tot;i++){
        if(b[i]>l){        
            break;
        }
        if(l%b[i]==0) c[++totb]=b[i];
    }
    for(int i=1;i<=totb;i++){
        if(c[i]>cnt){
            break;
        }
        for(int j=c[i];j<=cnt;j++){
            dp[j]|=dp[j-c[i]];
        }
    }
    if(dp[cnt]) {
        int now=cnt;
        for(int i=totb;i>=1;i--){
            if(c[i]>now){
                continue;
            }
            while(dp[now]&&dp[now-c[i]]){
                for(int j=now-c[i]+1;j<now;j++){
                    ans[a[j]]=a[j+1];
                }
                ans[a[now]]=a[now-c[i]+1];
                now-=c[i];
            }
            if(now==0) break;
        }
        int head=0,fir=0;
        for(int i=1;i<=n;i++){
            if(!ans[i]&&head==0){
                head=i;
                fir=i;
            }
            else if(!ans[i]&&head){
                ans[i]=head;
                head=i;
            }
        }
        ans[fir]=head;
        for(int i=1;i<=n;i++)
            cout<<ans[i]<<" ";
        cout<<"\n";
        
    }
    else{
        cout<<-1<<"\n";
    }


}
signed main(){
	IOS;
	int t=1;
	sieve();
    cin>>t;
	while(t--){
		solve();
       // cout<<"-------------------------"<<endl;
	}
	return 0;
}

2024/3/7 16:37
加载中...