sgu137:Funny Strings
来源:互联网 发布:Java 动态取得属性值 编辑:程序博客网 时间:2024/06/05 19:54
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 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 SN -1 在S'中。
观察可得,S2 S3 .. SN-1 为不变量,为了方便得到匹配,我们不妨把串的每一项先设为base=K/N,再继续根据需求加上1(总共要加K%N个1)。
怎么加1呢?我们首先来分析K%N=1的情况:
经过初始化后,S={base, base, base, base..., base},由于S1+1与SN -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;}
- sgu137:Funny Strings
- SGU137 Funny Strings
- SGU 137 Funny Strings
- sgu 137 Funny Strings
- [SGU]137. Funny Strings
- SGU 137 Funny Strings(构造)
- funny
- funny
- funny
- funny
- funny
- funny
- Funny Funny Game
- Funny Funny Game(数学题)
- Strings
- Strings
- strings
- Strings
- 拖动滚动条判断fixed定位切换时,火狐和IE下会有抖动,默认给定位的DIV一个absolute即可。
- Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)
- UVA1368
- Go语言开发环境配置
- 关于聚类分析、判别分析、主成分分析、因子分析等多元统计分析方法
- sgu137:Funny Strings
- Android 自定义控件实现刮刮卡效果 真的就只是刮刮卡么
- Unity3D-GUI基本控件
- 【测试人生】生生不息的人性化测试
- Android:ActionBar相关
- ggplot2作图详解:主题(theme)设置
- sgu107
- R语言基本统计分析方法(包及函数)
- mysql 5.6 cmake 模式安装