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+ ...
随机推荐
- Python之numpy基本指令
https://blog.csdn.net/mmm305658979/article/details/78745637 # -*- coding: utf-8 -*- 多加练习才是真 import n ...
- Python开发【Django】:日志记录、API认证
日志记录: 调用同一个对象,分别记录错误日志和运行日志 自定义日志类: class Logger(object): __instance = None def __init__(self): self ...
- 用 chown 和 chmod 修改目录所属用户及权限
1.修改 tmp 目录所属用户为 root,用户组为 root chown -R root:root /tmp12.修改 tmp 目录为可写权限 chmod -R 777 /tmp
- SQL基础--查询之一--单表查询
SQL基础--查询之一--单表查询
- Python知识总汇
一.python基础 python基础 python编码问题 逻辑运算 二.python数据类型 二.python数据类型 三.IO(文件处理) 三.IO(文件处理) 四.函数 函数基础 名称空间与作 ...
- C#——图片操作类简单封装
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- Linux /python --- zipinfo命令
Linux zipinfo命令用于列出压缩文件信息. 执行zipinfo指令可得知zip压缩文件的详细信息. zipinfo [-12hlmMstTvz][压缩文件][文件...][-x <范本 ...
- oracle定时器job的使用
对于DBA来说,数据库Job再熟悉不过了,因为经常要数据库定时的自动执行一些脚本,或做数据库备份,或做数据的提炼,或做数据库的性能优化,包括重建索引等等的工作.但是,Oracle定时器Job时间的处理 ...
- bzoj1612 / P2419 [USACO08JAN]牛大赛Cow Contest(Floyd)
P2419 [USACO08JAN]牛大赛Cow Contest Floyd不仅可以算最短路,还可以处理点之间的关系. 跑一遍Floyd,处理出每个点之间是否有直接或间接的关系. 如果某个点和其他$n ...
- 在pom.xml中使用distributionManagement将项目打包上传到nexus私服
本文介绍 如何在pom.xml中使用distributionManagement将项目打包上传到nexus私服 1.pom.xml文件添加distributionManagement节点 <!- ...