4th Jilin Province Collegiate Programming Contest - Mar 22, 2010
Problem C: The Natural Series
The natural series, from 1 to n and rewinding, has a special character. If you add
two values whose distance is 2, the sum can be divided by the middle value exactly.
Let’s check a example “1 2 3 4 5 6” (and rewinding to 1): (1+3)%2==0;
(2+4)%3==0; … ;(5+1)%6==0, and (6+2)%1==0. This character is unusual. You can’t
find another series when n is even, and only one different series essentially when n is
odd. For example, when n is 7, the other one is “1 2 3 7 4 5 6”.
Here we relax the constraints. If the sum of two numbers is less than the middle
value, this series can be accepted also. For example, “1 2 7 4 9 5 11 3 10 6 8” is true
where 5+3<11.
Input
Each line of input has two numbers, the first is n (4<=n<=16). The next positive
integer m (1<m<=n) is a reference that you must use it to be the second number in
result series.
Output
Print each series under above conditions in one line in sequence. The first
number must be 1, and second is the given reference. There is a blank between
adjacent numbers in one line and no blank after the last number. Print a blank
between each case and also no blank line after last case.
Sample Input
10 4
7 2
Sample Output
1 4 2 6 10 3 8 5 7 9
1 4 2 8 6 10 3 5 7 9
1 2 3 4 5 6 7
1 2 3 7 4 5 6
#include<iostream>
#include<cstring>
int n,m,vis[20],res[20];
void dfs(int dep)
{
if(dep==n+1)
{
if((res[n-1]+res[1])%res[n]==0||(res[n-1]+res[1])<res[n])
{
for(int i=1;i<n;i++) printf("%d ",res[i]);
printf("%d/n",res[n]);
}
return ;
}
for(int i=2;i<=n;i++)
{
if(vis[i]==0&&((res[dep-2]+i)%res[dep-1]==0||(res[dep-2]+i)<res[dep-1]))
{
vis[i]=1;
res[dep]=i;
dfs(dep+1);
vis[i]=0;
}
}
}
int main()
{
int f=0;
while(scanf("%d%d",&n,&m)==2)
{
if(f==0) f=1;else printf("/n");
memset(vis,0,sizeof(vis));
vis[1]=vis[m]=1;
res[1]=1,res[2]=m;
dfs(3);
}
return 0;
}
自然数列的特殊性质
本文探讨了一种特殊的自然数序列,该序列具有独特的数学特性:任选两个相隔一位的数,其和能被中间的数整除或小于中间的数。文章提供了一个示例并给出了解决方案的C++代码。

被折叠的 条评论
为什么被折叠?



