HDU multi-school 1003-Divide the Stones (construction)

Problem Description
There are n stones numbered from 1 to n.
The weight of the i-th stone is i kilograms. We divide the stones into k groups.
Each group consists of exactly stones.
We define the weight of each group is sum of the stones’ weights in the group.
Can we divide the stones so that the weights of all groups are same?
 

 

Input
The first line of input contains an integer T (1 <= T <= 100) denoting the number of test cases.
Each test case consists of one line containing two integers n (1 ≤ n ≤ 100000), k (k is divisor of n).
It is guaranteed that the sum of n overall test cases does not exceed 500000.
 

 

Output
For each test case, if you can’t divide into k groups satisfying condition, print “no”.
Else if you can divide into k groups satisfying condition, print “yes” in one line and then print k lines.
The i-th line represent the indices of stones belonging to the i-th group.
If there are multiple solutions, you can print any of them.
 

 

Sample Input
1 4 2
 

 

Sample Output
yes 1 4 2 3
 

 

Source
 

 

Recommend
chendu   |   We have carefully selected several similar problems for you:   6623  6622  6621  6620  6619 
The first problem with the summation formula of (1 - n) can be summed and then determines whether aliquot, not to be output to the output yes no 
When configured for printing how to partition the discussion If n / k is an even number when the press columns filled snake n / k is an odd number two behind the front row to be processed separately, but snake is also the problem before the two difficulty
 The first two columns you want to achieve the number 1 ---- 2k have to spend and ensure that every line of incremental and can +1 this time we can then find the value of the first row by the nature of the arithmetic sequence and then assigned the value 1 and another number can find out the former and + (k / 2 + 1) - (k / 2) ...... latter - (k / 2) + (k / 2 + 1) .... . put this question to achieve a structure

 

Code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+5;

typedef long long ll;
using namespace std;
vector<int>vec[maxn];
int vis[maxn];
int main()
{
    int T;
    cin>>T;
    ll n,k;
    while(T--)
    {
        scanf("%lld%lld",&n,&k);
        ll  sum=(n*(n+1)/2);
        for(int t=1; t<=k; t++)
        {
            vec[t].clear();
        }
        memset(vis,0,sizeof(vis));
        if(sum%k!=0)
        {
            puts("no");
        }
        else
        {
            if((n/k)%2==0)
            {
                puts("yes");
                int cnt=1;
                for(int j=1; j<=n/k; j++)
                {
                    if(j%2==1)
                    {
                        for(int t=1; t<=k; t++)
                        {
                            vec[t].push_back(cnt++);
                        }
                    }
                    else
                    {
                        for(int t=k; t>=1; t--)
                        {
                            vec[t].push_back(cnt++);
                        }
                    }
                }
                for(int t=1; t<=k; t++)
                {
                    for(int j=0; j<vec[t].size(); j++)
                    {
                        if(j!=vec[t].size()-1)
                            printf("%d ",vec[t][j]);
                        else
                        {
                            printf("%d",vec[t][j]);
                        }
                    }
                    printf("\n");
                }
            }
            else
            {
                puts("yes");
                if(n==1)
                {
                    puts("1");
                    continue;
                }
                ll kk=(3*k+3)/2;
                ll ss=k/2+1;
                
                int cnt=1;
                vec[1].push_back(cnt);
                vec[1].push_back(kk-cnt);
                int cnt1=cnt;
                int cnt2=kk-cnt;
                for(int t=2;t<=k;t++)
                {
                    if(t%2==0)
                    {
                    int temp1=cnt1+ss,temp2=cnt2-abs(k-ss);
                    vec[t].push_back(temp1);
                    vec[t].push_back(temp2);
                    cnt1=temp1;
                    cnt2=temp2;
                    }
                    else
                    {
                    int temp1=cnt1-abs(k-ss),temp2=cnt2+ss;
                    vec[t].push_back(temp1);
                    vec[t].push_back(temp2);
                    cnt1=temp1;
                    cnt2=temp2;
                    }
                    
                }
                cnt=2*k+1;
                for(int j=1; j<=n/k-2; j++)
                {
                    if(j%2==0)
                    {
                        for(int t=1; t<=k; t++)
                        {
                            vec[t].push_back(cnt++);
                        }
                    }
                    else
                    {
                        for(int t=k; t>=1; t--)
                        {
                            vec[t].push_back(cnt++);
                        }
                    }
                }
                for(int t=1; t<=k; t++)
                {
                    for(int j=0; j<vec[t].size(); j++)
                    {
                        if(j!=vec[t].size()-1)
                            printf("%d ",vec[t][j]);
                        else
                        {
                            printf("%d",vec[t][j]);
                        }
                    }
                    printf("\n");
                }
            }
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Staceyacm/p/11280622.html