ybtoj:递推算法
A:错排问题
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int w;
long long a[22];
int main()
{
cin>>w;
a[1]=0;
a[2]=1;
for(int i=3;i<=w;i++)
{
a[i]=(i-1)*(a[i-1]+a[i-2]);
}
cout<<a[w];
return 0;
}
B:传球游戏
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int n,m;
int dg[33][33];
int main()
{
cin>>n>>m;
for(int j=0;j<=m;j++)
{
for(int i=1;i<=n;i++)
{
if(i==1&&j==0)
{
dg[i][j]=1;
}
else if(i==1)
{
dg[i][j]=dg[n][j-1]+dg[i+1][j-1];
}
else if(i==n)
{
dg[i][j]=dg[i-1][j-1]+dg[1][j-1];
}
else{
dg[i][j]=dg[i-1][j-1]+dg[i+1][j-1];
}
}
}
cout<<dg[1][m];
return 0;
}
C:数的划分
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int dg[222][10];
int n,k;
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++)
{
if(i<j){
dg[i][j]=0;
}
else if(i==j){
dg[i][j]=1;
}
else{
dg[i][j]=dg[i-1][j-1]+dg[i-j][j];
}
}
}
cout<<dg[n][k];
return 0;
}
D:栈的问题
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int n;
int sq[20][20];
int main()
{
cin>>n;
for(int j=0;j<=n;j++)
{
for(int i=j;i<=n;i++)
{
if(j==0)
{
sq[i][j]=1;
}
else{
sq[i][j]=sq[i-1][j]+sq[i][j-1];
}
}
}
// for(int i=1;i<=n;i++)
// {
// for(int j=1;j<=n;j++)
// {
// cout<<sq[i][j]<<" ";
// }
// cout<<endl;
// }
cout<<sq[n][n];
return 0;
}
E:求f函数
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int n;
long long f[1000005];
int main()
{
for(int i=1000000;i>=1001;i--){
f[i]=i-10;
}
for(int i=1000;i>=1;i--){
f[i]=f[f[i+11]];
}
long long sum=0;
while(1)
{
scanf("%d",&n);
if(n==0){
break;
}
//cout<<f[n]<<" ";
sum = sum ^ f[n];
}
printf("%lld",sum);
return 0;
}
F:划分数列
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=100001;
int a[N];
int b=2;
int n;
int sum;
int main()
{
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<n;i++){
if(a[i]<a[i+1] && b==0){
sum++;
b=2;
}
else if(a[i]>a[i+1] && b==1){
sum++;
b=2;
}
else if(a[i]<a[i+1]){
b=1;
}
else if(a[i]>a[i+1]){
b=0;
}
else b=2;
}
cout<<sum+1<<endl;
return 0;
}
G:无限序列
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int q;
long long s[110], f[110], a, b;
inline long long solve(long long v) {
if (v == 0) return 0;
int max = 0;
for (int i = 90; i >= 1; i--) {
if (f[i] <= v) {
max = i;
break;
}
}
return s[max] + solve(v - f[max]);
}
int main() {
scanf("%d", &q);
f[1] = 1;
f[2] = 2;
s[1] = 1;
s[2] = 1;
for (int i = 3; i <= 109; i++) {
f[i] = f[i - 1] + f[i - 2];
s[i] = s[i - 1] + s[i - 2];
}
while (q--) {
scanf("%lld%lld", &a, &b);
cout << solve(b) - solve(a - 1)<<endl;
}
return 0;
}
I:等距跳跃
点击查看代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
int f=1,lzx=0;
char c=getchar();
while(c>'9' || c<'0'){
if(c=='-') f=-f;c=getchar();
}
while(c<='9' && c>='0'){
lzx=lzx*10+c-'0';c=getchar();
}
return lzx*f;
}
const int N=5e3+10;
struct point{
int x,y;
bool operator<(const point & p) const{
return x==p.x ? y<p.y : x<p.x;
}
}box[N];
inline int cmp(point x,point y){
if(x.x!=y.x) return x.x<y.x;
return x.y<y.y;
}
struct hash_pair{
template<class t1,class t2>
size_t operator()(const pair<t1,t2> & p) const{
auto hash1=hash<t1>{}(p.first);
auto hash2=hash<t2>{}(p.second);
return hash1^hash2;
}
};
int ans;//unordered_,hash_pair
map<pair<int,int>,int> mp[N];
int main(){
int R=read(),C=read(),n=read();
for(register int i=1;i<=n;i++){
box[i].x=read(),box[i].y=read();
}
sort(box+1,box+n+1);
for(register int i=1;i<=n;i++){
for(register int j=1;j<i;j++){
int xx=box[i].x-box[j].x,yy=box[i].y-box[j].y;
if(mp[j].find(make_pair(xx,yy))!=mp[j].end()){
int res=mp[j][make_pair(xx,yy)]+1;
mp[i][make_pair(xx,yy)]=res;
if(box[i].x+xx<=0 || box[i].x+xx>R || box[i].y+yy<=0 || box[i].y+yy>C){
ans=max(ans,res);
}
}
else if(box[j].x-xx<=0 || box[j].y-yy<=0 || box[i].y+yy>C || box[i].y+yy>R){
mp[i][make_pair(xx,yy)]=2;
}
}
}
if(ans>2) cout<<ans<<"\n";
else cout<<"0\n";
return 0;
}
//抄袭绝非良策,理解才是正道