Sgu 137. Funny string

Source: Internet
Author: User
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] << ‘ ‘;}

 

  

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.