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. CTE的妙用

    转自:https://blog.csdn.net/kk185800961/article/details/42535223 之前在2本书看到过with as 子句的一个简单例子,网上没找到相关资料. ...

  2. IOS使用Core-Plot画折线图

    关于Core-Plot的配置.大家能够參考我的上一篇博客:http://1.wildcat.sinaapp.com/?p=99 版权全部.转载请注明原文转自:http://blog.csdn.net/ ...

  3. 美图秀秀DBA谈MySQL运维及优化

    美图秀秀DBA谈MySQL运维及优化 https://mp.weixin.qq.com/s?__biz=MzI4NTA1MDEwNg==&mid=401797597&idx=2& ...

  4. java-mybaits-009-mybatis-spring-使用,SqlSessionFactoryBean、事务

    一.版本限制 参看地址:http://www.mybatis.org/spring/ 二.使用入门 2.1.pom <dependency> <groupId>org.myba ...

  5. 203-ReactDOM

    一.概述 加载方式: <script> ES6:import ReactDOM from 'react-dom' ES5:var ReactDOM = require('react-dom ...

  6. JS操作符转化数字

    在Node.js源代码里,随处可见使用各种符号处理字符串为数字的.可能由于不同人编写,使用的风格也各有不同. 基本上有下面几种. 将字符串转化为数字 + 将一个数字的字符串转化为数字很简单的一种做法就 ...

  7. node-schedule 实现定时任务使用方法记录

    在项目中有个每天0点执行的函数,本来想用setInterval来实现,但觉得这种需求以后应该还会有,自己写可能拓展性不高.搜了一下发现了node-schedule这个包.现在记录一下使用方法 node ...

  8. 多图片生成pdf文件

    这里记录多个图片合并生成一个pdf文件的方法. 首先maven引入所需jar包: <dependency> <groupId>com.itextpdf</groupId& ...

  9. google chrome插件开发,自己动手,丰衣足食

    因为平时上网都用chrome,但总感觉除了速度快,简洁以外总还有地方满足不了我的需要,然后找插件…后来发现,插件虽然海量但找个称心如意的也不是件容易的事儿,用在找插件的时间都能自己写一个了,于是,今年 ...

  10. 文字和图片居中的HTML代码怎么写?

    HTML 代码 ,怎么将文本/ 图片居中?这是在W3Cschool的编程问答中前端♌蕾儿提出的问题.网友施主同西否给出了详细的解答. html文字居中和html图片居中方法代码,通过在html中实现文 ...