题目大意:
由n个演员组成一个环,每组有n/d(约数)个人,在同一组相邻的人编号不同,要求按字典顺序的编号总和,无解输出Impossible。
思路:
先枚举字母,求出当前搜到的相邻的数,然后判断判断是不是字母,然后搜,如果没了,求出字符,跳出,然后求约数,再调用dfs就好了。
C o d e Code Code:
#include<cstdio>
#include<cstring>
#include<iostream>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
using namespace std;
int n,a[300005],h,r;
char zf[300005];
bool check;
void dfs (int dep)//搜字母
{
while (dep<=n)//如果有
{
for(int i='a';i<='z';++i)//枚举字母
{
check=false;//初始化
for(int j=1;j<=h;++j)//枚举约数
{
int r=dep-a[j],l=dep+a[j];//相邻
if(r<1)r+=n;//++
if(l>n)l-=n;//--
if(zf[l]==i||zf[r]==i){check=true;break;}
}
if(!check){zf[dep]=i;break;}
}
if(!check)dep++;
else
{
printf("Impossible");
return;
}
}
}
void ys()//求约数
{
for(int i=1;i*i<n;++i)
{
if(n%i==0)
{
a[++h]=i;
a[++h]=n/i;
if(n/i==i||i==1) --h;
}
}
}
int main()
{
fre(arilang);
scanf("%d",&n);
ys();
dfs(1);
if(!check)for(int i=1;i<=n;i++)cout<<zf[i];//输出
else printf("Impossible");
return 0;
}