sgu 137. Funny Strings 线性同余,数论,构造 难度:3
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 SN -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的更多相关文章
- 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 ...
- 解密随机数生成器(二)——从java源码看线性同余算法
Random Java中的Random类生成的是伪随机数,使用的是48-bit的种子,然后调用一个linear congruential formula线性同余方程(Donald Knuth的编程艺术 ...
- python3 线性同余发生器 ( random 随机数生成器 ) 伪随机数产生周期的一些探究
import random x=[str(random.randint(0, 5)) for i in range(10)] x_str=''.join(x) y=[str(random.randin ...
- hdu1573:数论,线性同余方程组
题目大意: 给定一个N ,m 找到小于N的 对于i=1....m,满足 x mod ai=bi 的 x 的数量. 分析 先求出 同余方程组 的最小解x0,然后 每增加lcm(a1...,am)都 ...
- [JXOI2018]游戏 (线性筛,数论)
[JXOI2018]游戏 \(solution:\) 这一道题的原版题面实在太负能量了,所以用了修改版题面. 这道题只要仔细读题,我们就可以将题目的一些基本性质分析出来:首先我们定义:对于某一类都可以 ...
- P2613 【模板】有理数取余 (数论)
题目 P2613 [模板]有理数取余 解析 简单的数论题 发现并没有对小数取余这一说,所以我们把原式化一下, \[(c=\frac{a}{b})\equiv a\times b^{-1}(mod\ p ...
- SGU 137.Funny String
题目描述 一个序列S1 S2 S3... Sn 如果满足 新序列 S1-1 S2 S3 ...Sn+1能够通过旋转的操作(不是翻转)来得到旧的序列,那么这个序列就叫做Funny序列.例如 1 2 1 ...
- 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 ...
- POJ 1426 Find the Multiple 思路,线性同余,搜索 难度:2
http://poj.org/problem?id=1426 测试了一番,从1-200的所有值都有long long下的解,所以可以直接用long long 存储 从1出发,每次向10*s和10*s+ ...
随机推荐
- CRM - 权限
一.引入权限组件 引入权限组件 rbac settings: 'rbac.apps.RbacConfig', 中间件: 'rbac.service.rbac.ValidPermission', 员工表 ...
- Django - 项目总结
总结: 基础,进阶,项目 Django - 练习(图书管理系统) 前后端分离得项目: vue + rest framework 项目: 图书增删改查页面 BBS + BLOG系统 CRM系统 在线 ...
- Find them, Catch them---poj1703(并查集)
题目链接:http://poj.org/problem?id=1703 可以认为n个人和m句话: 每句话包含A a b;D a b; 刚开始关系不确定: A a b 就是问ab 是否同类: D a b ...
- Python开发【Django】:Form组件
Form组件 Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 创建Form类时,主要涉及到 [ ...
- EOS Dapp开发(1)-基于Docker的开发环境搭建
随着EOS主网的上线,相信基于EOS的Dapp开发会越来越多,查阅了很多资料相关的开发资料都不是很多,只能自己摸索,按照网上仅有的几篇教程,先git clonehttps://github.com/E ...
- 用SQL语句检查CPU和磁盘空间
--查看4小时内的CPU变化值,1分钟统计一次 DECLARE @ts_now BIGINT; SELECT @ts_now = ms_ticks FROM sys.dm_os_sys_info; - ...
- 005-spring cache-配置缓存存储
一.概述 缓存抽象提供了多种存储集成.要使用它们,需要简单地声明一个适当的CacheManager - 一个控制和管理Caches的实体,可用于检索这些实体以进行存储. 1.1.基于JDK Concu ...
- python 实现的比特币代码 及 加密货币学习线路图及书籍资料
http://www.pycoind.org/ https://github.com/samrushing/caesure https://bitcointalk.org/index.php?topi ...
- Mybatis 创建Configuration对象
Mybatis 创建Configuration对象是在项目启动时就创建了. 具体创建流程为: https://blog.csdn.net/wolfcode_cn/article/details/80 ...
- OpenCV Windows7 VC6.0安装以及HelloWorld
anna在实验室配置OpenCV的时候,按照中文网站的介绍,很顺利的就完成了.可是回到家情况就大不一样!!总是在链接的时候报错,不是少这个lib就是少那个lib大哭最后查明是anna马虎,忘了将C:\ ...