CodeforceC1, C2-k-LCM (Thinking)

easy topic link

hard topic link


Title:

The two questions almost give you an n and k, and divide n into k parts

  1. Make the sum of k numbers n.
  2. Make the number of k lcm less than or equal to n/2

The k of the easy version is 3 and the k of the
hard version is less than equal to n greater than or equal to 3.

Ideas:

Let's talk about the easy version first, because it is divided into three numbers, so we can judge whether it is an even number.

  • If it is not an even number, just take out a 1. The rest is evenly divided.
  • If it is an even number, then we can judge whether it can be divided into four equal parts. If we can, we can divide it into 1, 1, 2, so that both conditions can be met, and it cannot be divided into four equal numbers. Then we know that n is an even number and the part is divided into four equal parts then n /2 is an odd number, so we first take out a 2, and the rest will be divided equally, and the halved must be an even number, that is, lcm is the number of halves, and it will not exceed n/2.

Looking at the hard version again, he has changed from 3 copies to k copies, but we can still find the sameness, that is, take out 3 copies to transform the part of n, and it becomes the easy version. Pass 1 and any number If lcm is itself, we can think that we can take out (k-3) 1s first. Then we have 3 points left, and the processing of (n-k+3) will be converted into an easy version.

	ll t;
    cin >> t;
    while(t--)
    {
    
    
    	///easy版
    	cin>>n>>m;
    	if(n%2){
    
    
    		cout<<1<<" "<<n/2<<" "<<n/2<<endl;
    	}else {
    
    
    		if(n%4){
    
    
    			cout<<2<<" "<<n/2-1<<" "<<n/2-1<<endl;
    		}else {
    
    
    			cout<<n/2<<" "<<n/4<<" "<<n/4<<endl;
    		}
    	}
    	/*
    	///hard版    	
    	cin>>n>>m;	
        n -= (m - 3);///独立除出来三个 
        ///独立出来三个就剩下的平分,可以保证lcm小于等n/2
        for(int i = 1; i <= m - 3; i++) cout << 1 << " ";/// 先把前面的整上1
        
        ///多一 直接半分就行
        if(n % 2) cout << 1 << " " << n / 2 << " " << n / 2 << endl;
        else///偶数
        {
            if(n % 4)/// 因为是偶数,不能整除4,那么n/2 一定是一个奇数
            {
                cout << 2 << " " << n / 2 - 1 << " " << n / 2 - 1 << endl;
            }
            else///能够整除那么可以分成4份
                cout << n / 2 << " " << n / 4 << " " << n / 4 << endl;
        }*/
    }

Guess you like

Origin blog.csdn.net/weixin_45911397/article/details/114988963