跳到正文

题解

题解:P15834 [蓝桥杯第一届国际赛] 网站扩张

luogu_leo约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年3月21日
收录
2026年5月15日
更新
2026年11月9日
篇幅
约 2 分钟
题目
P15834

版本

  1. v2当前
  2. v1

题目传送门

  • 题目简述:

    一开始有一个用户第8天时可以邀请一个新用户,接下来每过两天就继续可以邀请一个新用户。

    新用户同理。

  • 思路简述:

遍历每一天,对每个存在的用户,检查当天是否满足以下条件:

  • 存在满 88 天;
  • 首次邀请在存在后第 88 天,之后每隔 22 天 ;

如果满足条件,新增一个用户(ansans++),并加入递归; 最终第 nn 天结束时输出ansans。

code:

C++
#include<bits/stdc++.h>
using namespace std;
int n,ans=1;
//cnt代表当前此用户已经存在了多少天
//day代表总天数
void dfs(int cnt,int day){
	if(day>n) return;//判断是否超过天数
	int fst=cnt+7;
	if(day<fst){
		dfs(cnt,day+1);
		return;
	}
	if((day-fst)%3==0){
		ans++;
		dfs(day,1);
	}
	dfs(cnt,day+1);
}
int main(){
	cin>>n;
	dfs(1,1);
	cout<<ans;
}

但是TLE…

因为当n=300n=300时,递归的调用次数会指数级增长,所以不能使用函数递归。

所以可以用数组直接统计 每一天有多少个用户能发出邀请,直接O(n)O(n)时间 ++ O(n)O(n)空间

code:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long//不开long long见祖宗
int ans=1;//ans=1因为刚开始有一个人
signed main(){
	int n;
	cin>>n;
	int cnt[312]={0};//cnt[i]=第i天有cnt[i]个发出邀请
	cnt[8]=1;//第8天第1次邀请
	for(int day=1;day<=n;day++){
		ans+=cnt[day];//ans+第day天的cnt
		if(day+7<=n){
			cnt[day+7]+=cnt[day];//第1次
		}
		if(day+3<=n){
			cnt[day+3]+=cnt[day];//第n次
		}
	}
	cout<<ans;//输出结束
}

不开long long见祖宗,十年OI一场空

原文