sgu137:Funny Strings

来源:互联网 发布:Java 动态取得属性值 编辑:程序博客网 时间:2024/06/05 19:54

137. Funny Strings

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

Let's consider a string of non-negative integers, containing N elements. Suppose these elements are S1 S2 .. SN, in the order in which they are placed inside the string. Such a string is called'funny' if the string S1+1 S2 S3 .. SN-1 S-1 can be obtained by rotating the first string (to the left or to the right) several times. For instance, the strings 2 2 2 3 and 1 2 1 2 2 are funny, but the string 1 2 1 2 is not. Your task is to find a funny string having N elements, for which the sum of its elements (S1+S2+..+SN) is equal to K.

Input

The input contains two integers: N (2<=N<=1000) and K (1<=K<=30000). Moreover, GCD(N,K)=1 (it can be proven that this is a necessary condition for a string to be funny).

Output

You should output one line containing the elements of the funny string found. These integers should be separated by blanks.

Hint

GCD(A,B) = the greatest common divisor of A and B. 
The 'funny' strings are also named Euclid strings in several papers.

Sample Input

9 16

Sample Output

1 2 2 2 1 2 2 2 2
一道被水过的题...(注意“被”字)
首先,rotate指的是S1S2S3...Sn变为SnS1S2S3...Sn-1类似的。
我们分析,若一个串为funny串,为了方便起见,在其后面复制一遍该串,得到新串S',那么满足序列 S1+1 S2 S3 .. SN-1 S-1 在S'中。
观察可得,S2 S3 .. SN-1 为不变量,为了方便得到匹配,我们不妨把串的每一项先设为base=K/N,再继续根据需求加上1(总共要加K%N个1)。
怎么加1呢?我们首先来分析K%N=1的情况:
经过初始化后,S={base, base, base, base..., base},由于S1+1S-1均在原串中,不妨将S1=base,Sn=base+1,那么这个串显然满足要求(因为S'中可以从第n项开始找到这个串)。
当K%N=2时,S={base, base, base, base..., base+1}(根据题意和这种构造法必须满足Sn=base+1),还需要加上一个1。
经分析,很容易发现当1加在Sn/2上时,S'中从第n/2项开始可以找到这个串。
当K%N=3时,依此类推...
由此我们得出加1的规律,设mod=K%N,那么S(1+i*N/mod)=base+1,1+i*N/mod∈(1,n),且Sn=base+1,其余的Sk=base。
#include <cstdio>using namespace std;int N, K;int A[1005] = {0};int main(){  int base = 0, mod = 0;  scanf("%d%d", &N, &K);    base = K/N;  mod = K%N;  for(int i = 1; i <= N; ++i) A[i] = base;  for(int i = 1; i*N/mod <= N; ++i) A[1+i*N/mod]++;  A[N]++;    printf("%d", A[1]);  for(int i = 2; i <= N; ++i) printf(" %d", A[i]);  printf("\n");  return 0;}


0 0
原创粉丝点击