2677: Problem C

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值