512 solutions

  • 2
    @ 2022-7-22 23:05:11

    奇妙的剪枝

    #include<bits/stdc++.h>
    using namespace std;
    #define ioio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define debug(x) cout<<#x<<":"<<x<<endl;
    #define endl "\n"
    #define P pair
    #define P1 first
    #define P2 second
    #define p_queue priority_queue
    #define u_map unordered_map
    typedef long long ll;
    const double eps=1e-6;
    const int mod=1e9+7;
    const int INF=0x3f3f3f3f;
    const int N=1e5+7;
    int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
    /*------------------------------------------*/
    
    int n,m;
    int a[N];
    int flag,cnt;
    vector<vector<int> >ma(21);
    void dfs(int u);
    
    void dfs_a(int x,int u,int len){
    	if(x>=len-1){
    		dfs(u+1);
    		return ;
    	}
    	ma[u+1].push_back(ma[u][x]+ma[u][x+1]);
    	dfs_a(x+1,u,len);
    	ma[u+1].pop_back();
    	ma[u+1].push_back(ma[u][x]-ma[u][x+1]);
    	dfs_a(x+1,u,len);
    	ma[u+1].pop_back();
    	ma[u+1].push_back(ma[u][x+1]-ma[u][x]);
    	dfs_a(x+1,u,len);
    	ma[u+1].pop_back();
    }
    void dfs(int u){
    	cnt++;
        //cnt搜索次数足够大时,大胆认为无解了,5000000是试出来的一个比较合理的数字
    	if(flag||cnt>=5000000)return ;
    	int len=ma[u].size();
    	if(len==1){
    		if(ma[u][0]==0)flag=1;
    		return ;
    	}
    	dfs_a(0,u,len);
    }
    int main(){
    	ioio
    	cin>>n>>m;
    	for(int i=1;i<=n+m;i++){
    		cin>>a[i];
    		ma[1].push_back(a[i]);
    	}
    	dfs(1);
    	if(flag)cout<<"Yes"<<endl;
    	else cout<<"No"<<endl;
    	return 0;
    }
    

    Information

    ID
    6504
    Time
    2000ms
    Memory
    512MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    123
    Accepted
    4
    Uploaded By