Backward Digit Sums
事情先是这样的:#include <iostream>
using namespace std;
int b[15];
int OK=1;
void sumpermutation(int* a,int n)
{
for(int i=n-1;i>0;i--)
{
for(int j=0;j<i;j++)
{
a[j]=a[j]+a[j+1];
}
}
}
void permutation(int* a,int *vis,int n,int cur,int m)
{
if(n==cur)
{
for(int i=0;i<n;i++)
b=a;
sumpermutation(b,n);
if(b[0]==m)
{
if(OK) //不输出了,但还在DFS
{
for(int i=0;i<n-1;i++)
cout<<a<<" ";
cout<<a[n-1];
cout<<endl;
OK=0;
}
return ;
}
}
else
{
for(int j=1;j<=n;j++)
{
if(vis[j]==0)
{
a[cur]=j;
vis[j]=1;
permutation(a,vis,n,cur+1,m);
vis[j]=0;
}
}
}
}
int main()
{
int a[10];
int vis[11]={0};
int m,n;
cin>>n>>m;
OK=1;
permutation(a,vis,n,0,m);
return 0;
}
结果:Time Limit Exceeded
还是用STL试试吧:
还是用STL试试吧:
#include <iostream>
#include <algorithm>
using namespace std;
int OK=1;
void sumpermutation(int* a,int n)
{
for(int i=n-1;i>0;i--)
{
for(int j=0;j<i;j++)
{
a[j]=a[j]+a[j+1];
}
}
}
int main()
{
int a[11]={1,2,3,4,5,6,7,8,9,10};
int b[10];
int m,n;
cin>>n>>m;
OK=1;
while(OK==1)
{
for(int i=0;i<n;i++) b=a;
sumpermutation(b,n);
if(b[0]==m)
{
for(int j=0;j<n;j++)
cout<<a[j]<<" ";
cout<<endl;
OK=0;
}
else
next_permutation(a,a+n);
}
return 0;
}
(果然ACCEPT了)