题目链接: E-Easy Construction
Description
题意:给出n, k, 求是否存在连续任意个数的和模n为k,并输出序列
Input
- The first line contains two integers n, k(1 <= n <= 5000, 0 <= k < n).
Output
Print n integers, the answer permutation in one line if such permutation exists, or print “-1” in one line if no solution exists.
Sample Input
2 1
Sample Output
1 2
Method
- 显然要满足n个连续的数之和对n取模为K,则要保证 (n*(n+1)/2)%n == k, 而当满足这个条件之后,对于连续1~n,是一定存在 连续i个数的和模n为k的(可以动手试验一下);
- 得到这个规律之后,再对n的奇偶进行分析即可;
- 对于偶数: { n/2, n, 1, n-1, 2, n-2, … }
- 对于奇数: { n, 1, n-1, 2, n-2, … }
Code
详见注释
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
#define ios std::ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);
#define rtxt freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define ll long long
const int Max = 1e6+3;
const int mod = 1e9+7;
int main()
{
ios
int n, k;
cin >> n >> k;
if(k != (n*(n+1)/2)%n) cout << -1 << endl;
else {
if(n&1) {
for(int i=1; i<=n/2; i++)
cout << i << " " << n-i << " ";
cout << n << endl;
}
else {
for(int i=1; i<n/2; i++)
cout << i << " " << n-i << " ";
cout << n/2 << " " << n << endl;
}
}
return 0;
}
蒟蒻一只,欢迎指正