1 1052 全排列
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int a[25];
bool vis[25];
int n;
int ans=0;
void f(int x){
if(x==n+1){
ans++;
//if(ans<=3){
for(int i=1;i<=n;i++){
printf("%5d",a[i]);
}
printf("
");
//}
return;
}
for(int i=1;i<=n;i++)
{
if(vis[i]==1) continue;
bool q=1;
//for(int j=1;j<x;j++)
//{
// if(abs(x-j)==abs(i-a[j])){
// q=0;
// break;
// }
//
//}
if(q){
vis[i]=1;
a[x]=i;
f(x+1);
vis[i]=0;
}
}
return ;
}
int main( ){
scanf("%d",&n);
f(1);
//printf("%d",ans); //...
return 0;
}
2 1046
题意
(m,n∈[1,k]∩Z)
((n^2-mn-m^2)^2=1)
你的任务是:输入正整数(k(1≤k≤109)),求一组满足上述两个条件的(m,n),并且使(m^2+n^2)的值最大。例如,从键盘输入(k=1995),则输出:(m=987,n=1597)。
题解
根据
[(n^2-mn-m^2)^2=1
]
有
[(m^2+nm-n^2)^2=1
]
又因为
[m^2+nm-n^2=(m+n)^2-mn-2n^2
]
[=(m+n)^2-(m+n)n-n^2
]
所以
[(n^2-mn-m^2)^2=((m+n)^2-(m+n)n-n^2)^2
]
所以看出来如果(m)和(n)是一组解那么(m+n)和(n)也是一组合法解
枚举第一组可以知道这(m)和(n)就是(FIB)的相邻两项
所以题目变成了了求小于(k)的最大的(FIB)相邻两项
未AC原因
#include<cstdio>
long long a,b,c,k;
int main(){
scanf("%lld",&k);
a=1,b=1,c=a+b;
while (c<k)
{
a=b;
b=c;
c=a+b;
}
printf("m=%lld
n=%lld
",a,b);
return 0;
}
题外话
(m,nin [1,k])
(n^2-m^2=1+mn)
((m^2+n^2)^2=5m^2n^2+2mn+1=5x^2+2x+1=5(x+dfrac{1}{5})^2+dfrac{4}{5})
(n^2-mn-m^2=-1)
((m^2+n^2)^2=3m^2n^2+1=3x^2+1)