1 solutions

  • 0
    @ 2025-12-3 8:28:20

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    long long g,n,l,u,ans=9e18,tmp;
    int main(){
    	cin>>g;
    	while(g--)
    	{
    		ans=9e18;
    		cin>>n>>l>>u;
    		if(l==0){
    			if(u*100000>n)
    				if(n>=u) cout<<n%u<<endl;
    				else cout<<-1<<endl;
    			else cout<<n-u*100000<<endl; 
    		}
    		else if(u==0){
    			if(l*100000>n) 
    				if(n>=l) cout<<n%l<<endl;
    				else cout<<-1<<endl;
    			else cout<<n-l*100000<<endl;
    		}
    		else if(l*2+u>n) cout<<-1<<endl;//最少情况:男生2,女生1 
    			else {//枚举每个男生糖果	
    				for(long long i=100000;i>=1;i--)
    				{				
    					long long yu=n-i*l;//计算余下的糖果 
    					if(yu>=u){//满足每个女生至少有一颗 
    						long long s1=yu/u;
    						long long s2=min(s1,i-1); 
    						tmp=yu-s2*u;//求分完给女生后剩下的 
    						ans=min(ans,tmp);
    					//	cout<<i<<" "<<yu<<" "<<s1<<" "<<s2<<" "<<tmp<<" "<<ans<<endl;
    					}
    				}
    				cout<<ans<<endl;
    			}
    	}
        return 0;
    }
    
    • 1

    Information

    ID
    745
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    10
    Tags
    # Submissions
    3
    Accepted
    1
    Uploaded By