Description
A sequence S1 S2 s3... SN if the new sequence S1-1 S2 S3 is satisfied... the Sn + 1 can get the old sequence through the rotation operation (not the flip), so this sequence is called the funny sequence. For example, 1 2 1 2 2 is the funny sequence, and 1 2 1 2 is not
Input
Given a number N, it indicates the sequence length. A k represents the sum of the numbers in the sequence. Gcd (n, k) = 1 (this can ensure that this question is answered)
Output
The number of N rows indicates a sequence that meets the conditions.
Sample Input
9 16
Sample output
1 2 2 2 1 2 2 2 2
Solution:
Assume that the original string S is, S0, s1...... Sn-1.
After rotating t times, St, ST + 1... St-1
According to the relationship,
S0 = ST, S1 = ST + 1,
That is, SI = SJ, which is valid when I mod T = J nod T.
After reading N, and K, make all the elements in s p = K/n
Then K mod n is left, which is not allocated.
From St = S0 + 1, zhist = S2 * t = Sm * t = p + 1;
Sn-1 = S0 + 1 = p + 1;
Make d = K mod N, that is, the number of 1 to be allocated is d,
When D * T % N = n-1, all d s are allocated.
Then, enumerate t to determine the size of T.
Confirm the elements of 1 and then output them.
Reference code:
#include <iostream>using namespace std;int n, k, t;int f[1000];int main() {cin >> n >> k;for (t = 1; t < n; t++) if ( (n - 1) == (k % n) *t % n) break;for (int i = t; i != n - 1; i = (i + t) % n)f[i] = 1;f[n - 1] = 1;for (int i = 0; i < n; i++) cout << k / n + f[i] << ‘ ‘;}