HDU多校1003-Divide the Stones(構造)

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 
這個題先用求和公式對(1——n)求和 而後判斷是否能進行整分,不行就輸出no 能夠就輸出yes 
對於打印的時候怎麼構造是要分狀況討論的若n/k是偶數 則按列進行蛇皮填 若n/k是奇數 要單獨處理前兩列後面進行蛇皮排 但也是前兩列是這個題的難點
 前兩列 你要實現把 1----2k個數都用上 而且保證每行之和可以遞增+1 這時候咱們能夠經過等差數列的性質 而後求出第一行的值而後賦值爲 1 和 另外一個能夠求出來的數 而後前者+(k/2+1) -(k/2)...... 後者-(k/2)+(k/2+1).....就把這個題的構造實現了

 

代碼:
#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; }
相關文章
相關標籤/搜索