sgu137:Funny Strings

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值