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 2is 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

首先k/n的部分可以平均分配,比如9,16,每个元素都先+1,接下来就只有0,1需要分配了

接着,因为gcd(n,k)==1,所以必然存在一个数gap在[1,n-1]内,使[0,n-1]所有整数temp都被[gap*temp%n]遍历(为了防止有重合),且恰好使k*gap%n==n-1的
然后根据[(gap*i)%n]遍历到的第i项(1<=i<=k)都赋1,其它的都赋0就行了
也就是向右旋转gap次,恰好都落在下一个遍历的位置上,第0个位置赋1,第k个位置赋0,其他位置都不变,这样恰好翻转
从题目的方面,所有的位置必然处在gap*temp循环之内否则会有a[0]==a[n-1]出现不满足题意

#include<cstdio>
using namespace std;
int bit[35000];
int main(){
int n,k;
scanf("%d%d",&n,&k);
int t=k/n;
k%=n;
int gap=1;
for(int i=1;i<n;i++){
if((long long)i*k%n==n-1){
gap=i;
break;
}
}
for(int i=1;i<=k;i++){
bit[(long long )(gap*i)%n]=1;
}
for(int i=0;i<n;i++){
printf("%d%c",t+bit[i],i==n-1?'\n':' ');
}
return 0;
}

  

												

sgu 137. Funny Strings 线性同余,数论,构造 难度:3的更多相关文章

  1. SGU 140. Integer Sequences 线性同余,数论 难度:2

    140. Integer Sequences time limit per test: 0.25 sec. memory limit per test: 4096 KB A sequence A is ...

  2. 解密随机数生成器(二)——从java源码看线性同余算法

    Random Java中的Random类生成的是伪随机数,使用的是48-bit的种子,然后调用一个linear congruential formula线性同余方程(Donald Knuth的编程艺术 ...

  3. python3 线性同余发生器 ( random 随机数生成器 ) 伪随机数产生周期的一些探究

    import random x=[str(random.randint(0, 5)) for i in range(10)] x_str=''.join(x) y=[str(random.randin ...

  4. hdu1573:数论,线性同余方程组

    题目大意: 给定一个N ,m 找到小于N的  对于i=1....m,满足  x mod ai=bi  的 x 的数量. 分析 先求出 同余方程组 的最小解x0,然后 每增加lcm(a1...,am)都 ...

  5. [JXOI2018]游戏 (线性筛,数论)

    [JXOI2018]游戏 \(solution:\) 这一道题的原版题面实在太负能量了,所以用了修改版题面. 这道题只要仔细读题,我们就可以将题目的一些基本性质分析出来:首先我们定义:对于某一类都可以 ...

  6. P2613 【模板】有理数取余 (数论)

    题目 P2613 [模板]有理数取余 解析 简单的数论题 发现并没有对小数取余这一说,所以我们把原式化一下, \[(c=\frac{a}{b})\equiv a\times b^{-1}(mod\ p ...

  7. SGU 137.Funny String

    题目描述 一个序列S1 S2 S3... Sn 如果满足 新序列 S1-1 S2 S3 ...Sn+1能够通过旋转的操作(不是翻转)来得到旧的序列,那么这个序列就叫做Funny序列.例如 1 2 1 ...

  8. POJ 3087 Shuffle'm Up 线性同余,暴力 难度:2

    http://poj.org/problem?id=3087 设:s1={A1,A2,A3,...Ac} s2={Ac+1,Ac+2,Ac+3,....A2c} 则 合在一起成为 Ac+1,A1,Ac ...

  9. POJ 1426 Find the Multiple 思路,线性同余,搜索 难度:2

    http://poj.org/problem?id=1426 测试了一番,从1-200的所有值都有long long下的解,所以可以直接用long long 存储 从1出发,每次向10*s和10*s+ ...

随机推荐

  1. Python开发【模块】:time、datatime

    时间模块 时间相关的操作,时间有三种表示方式: 时间戳               1970年1月1日之后的秒,即:time.time() 格式化的字符串    2014-11-11 11:11,   ...

  2. 双态运维分享之:业务场景驱动的服务型CMDB

    最近这几年,国内外CMDB失败的案例比比皆是,成功的寥寥可数,有人质疑CMDB is dead?但各种业务场景表明,当下数据中心运维,CMDB依然是不可或缺的一部分,它承载着运维的基础,掌握运维的命脉 ...

  3. 用 chown 和 chmod 修改目录所属用户及权限

    1.修改 tmp 目录所属用户为 root,用户组为 root chown -R root:root /tmp12.修改 tmp 目录为可写权限 chmod -R 777 /tmp

  4. http和socket之长连接和短连接区别

    TCP/IP TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层. 在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议. 在传输层中有TCP协议与UDP协议. 在应 ...

  5. 10.numpy基本用法

    参考: https://blog.csdn.net/sinat_32547403/article/details/54017551

  6. github使用方法

    1:fork别人的项目后,更新别人最新的提交 https://blog.csdn.net/qq1332479771/article/details/56087333 2:在idea中push工程    ...

  7. cocos代码研究(18)Widget子类Slider学习笔记

    理论基础 滑动条控件. 代码实践 实例: // Create the slider Slider* slider = Slider::create(); slider->loadBarTextu ...

  8. node核心:异步流程控制

    Node.js的异步是整个学习Node.js过程中重中之重. 1)异步流程控制学习重点 2)Api写法:Error-first Callback 和 EventEmitter 3)中流砥柱:Promi ...

  9. VS2010/MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)

    言归正传,鸡啄米上一节中讲了编辑框的用法,本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box ...

  10. Spring的一些面试题(转)

    一.spring工作原理: 1.spring mvc的所有请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责对请求进行真正的处理工作.2.DispatcherServlet ...