牛客多校(2020第六場)E Easy Construction
阿新 • • 發佈:2020-08-17
示例1
輸入
2 1
輸出
1 2
說明
The sum of subintervals [1],[1,2][1],[1,2][1],[1,2] both satisfy ≡1(mod2), and their lengths are1,21,21,2 respectively.
示例2
輸入
3 1
輸出
-1
題解:
- 如果有解,那麼 n(n+1)/2 % n == k,因為長度為n的子區間是P本身,P的元素之和為n(n+1)/2.
- 假設k滿足上述條件,此時一定有解。如果是奇數,則k=0,則P={n, 1, n-1, 2, n-2, ..},若為偶數,則P={n, n/2, 1, n-1, 2, n-2, ...}
1 #include<iostream> 2 using namespace std; 3 4 int n, k; 5 6 void solve() { 7 cin >> n >> k; 8 int sum = n * (n+1) / 2; 9 if (sum % n != k) cout << "-1\n"; 10 else { 11 if (n % 2 == 0) { 12 cout << n << " " << n / 2; 13 for (int i = 1; i <= n/2 -1; i++) { 14 cout << " " << i << " " << n - i; 15 } 16 } 17 else { 18 cout << n; 19 for (int i = 1; i <= n/2; i++) { 20 cout << " " << i << "" << n - i; 21 } 22 } 23 } 24 } 25 26 int main() { 27 solve(); 28 cout << "\n"; 29 return 0; 30 }