跳到正文

个人记录

Informatics Dream Team Regular Contest 005 题解

why110623约 13 分钟
原文
文章信息与版本

文章信息

发布
2025年5月11日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 13 分钟

版本

  1. v1当前

A

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5;
void solve() {
	int n;
	cin >> n;
	if (n & 1)cout << (n + 1) / 2;
	else cout << n / 2;

}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t = 1;
	//cin>>t;
	while (t--) {
		solve();
	}
	return 0;
}

B

如果存在连续两盏亮着的灯,就灭掉后面那盏

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5;

void solve() {
	int n;
	cin >> n;
	string s;
	cin >> s;
	int res = 0;
	for (int i = 1; i < n; i++) {
		if (s[i] == '1' && s[i - 1] == '1') {
			s[i] = '0';
			res++;
		}
	}
	cout << res;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t = 1;
	//cin>>t;
	while (t--) {
		solve();
	}
	return 0;
}

C

每次可以灭掉连续的K盏灯,所以一共有ans=n-k+1种操作方式,因此会产生2的ans次方种灯的状态,数据较小,不需要快速幂。

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5;
const int mod = 1e9 + 7;

void solve() {
	int n, k, res = 1;
	cin >> n >> k;
	int ans = n - k + 1;
	for (int i = 1; i <= ans; i++) {
		res = res * 2 % mod;
	}
	cout << res;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t = 1;
	//cin>>t;
	while (t--) {
		solve();
	}
	return 0;
}

D

与C题不同的是,每次操作可以改变任意K盏灯的状态,限制条件减少,重复情况过多,C题方法不适用。

打表发现当k为奇数时,可以通过奇数次操作可以使得灭掉奇数盏灯,通过偶数次操作可以使得灭掉偶数盏灯,所有灯的状态都能被实现,即2的n次方种。

而当k为偶数时,任意次操作都只能灭掉偶数盏灯,所有只能实现所有情况的一半。

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
const int mod = 1e9 + 7;

void solve() {
	int n, k, res = 1;
	cin >> n >> k;
	if (k == n) {
		cout << 2;
		return;
	}
	int ans = n;
	if (k%2==0)ans--;
	for (int i = 1;i <= ans; i++) {
		res = res * 2 % mod;
	}
	cout << res;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t = 1;
	//cin>>t;
	while (t--) {
		solve();
	}
	return 0;
}

E

思维题

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int lowbit(int x)
{
	return x&-x;
}
int count1(int x)
{
	int ans=0;
	while(x)
		ans++,x-=lowbit(x);
	return ans;
}
signed main()
{
	for(int i=0;i<(1<<9);i++)
	{
		int a=0;
		if(i&256)
			a^=160;
		if(i&128)
			a^=336;
		if(i&64)
			a^=136;
		if(i&32)
			a^=276;
		if(i&16)
			a^=170;
		if(i&8)
			a^=81;
		if(i&4)
			a^=34;
		if(i&2)
			a^=21;
		if(i&1)
			a^=10;
		if(count1(a)==4)
		{
			for(int j=8;j>=0;j--)
			{
				if(i&(1<<j))
					cout<<1;
				else
					cout<<0;
			}
			cout<<"\n";
		}
	}
	return 0;
}
//by haozhengrui

F

树形dp,返回三个值:

  • 当前节点为开状态的最小操作数
  • 当前节点为关状态,且没有借时的最小操作数
  • 当前节点为关状态,且有借时的最小操作数

所谓借,就是先关闭,回到父节点时,再还回来,即同时关闭父节点.

Python
n = int(input())
from collections import defaultdict
road = defaultdict(list)
for _ in range(n-1):
    x,y = map(int,input().strip().split())
    road[x].append(y)
    road[y].append(x)
from types import GeneratorType 
def bootstrap(f, stack=[]):   #yield
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc
# 当前节点,开,关不欠,关欠
@bootstrap
def dfs(node,last):
    a,b,c = 0,0,1
    dmn = float("inf")
    tmpe,tmpc = 0,float("inf")
    for nxt in road[node]:
        if nxt == last:
            continue
        la,lb,lc = yield dfs(nxt,node)
        # a开,子节点都关闭
        a += lb
        # b关不欠,要选一个lc
        '''
        le1 = min(la,lb)
        le1 + lc2 < lc1 + le2
        lc2 - le2 < lc1 - le1
        '''
        le = min(la,lb)
        c += le
        d = lc - le
        if d < dmn:
            dmn = d            
            tmpe,tmpc = le,lc
        b += le
    b -= tmpe
    b += tmpc
    return (yield a,b,c)
a,b,c = dfs(1,0)
print(min(a,b))

G

光线从光源 (SX,SY)(S_X,S_Y) 开始,可以朝任意角度射出。

计算光线与圆周的交点,该点可以表示为参数化形式的 (cos(x),sin(x)),其中是 x 射出的角度。

首先,判断光线是否与柱子相交,如果光线在到达圆周前首先与柱子相交,则该方向上的光线将不会到达圆周。

根据给定的最大反射次数 K,逐步计算出可以利用镜面反射到达的额外区域。

每次反射后,检查新的路径与圆周及柱的交点。

将所有可到达的外周点的总长度与正圆周长 (2π) 进行比值计算,得出比例。

C++
#include<bits/stdc++.h>
using namespace std;
#define rep(index,num) for(int index=0;index<num;index++)
#define rep1(index,num) for(int index=1;index<=num;index++)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define eps 1e-7
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl=1e+18+1;
int INF=1e+9+1;
long double nibutan(long double inx,long double iny,long double outx,long double outy){
    long double midx=(inx+outx)/2,midy=(iny+outy)/2;
    if(sqrt(pow(inx-outx,2.0)+pow(iny-outy,2.0))<eps) return atan2(midy,midx);
    if(midx*midx+midy*midy<1.0) return nibutan(midx,midy,outx,outy);
    else return nibutan(inx,iny,midx,midy);
}
int main(){
    long double X,Y,R,SX,SY;
    int K;
    scan(X);scan(Y);scan(R);scan(SX);scan(SY);
    scan(K);
    long double CWX=SX,CWY=SY,CCX=SX,CCY=SY;
    long double baseCWangle=0,baseCCangle=0;
    rep(i,K+1){
        long double CWbtheta=atan2(Y-CWY,X-CWX),CCbtheta=atan2(Y-CCY,X-CCX);
        long double CWkyori=sqrt(pow(Y-CWY,2.0)+pow(X-CWX,2.0)),CCkyori=sqrt(pow(Y-CCY,2.0)+pow(X-CCX,2.0));
        long double CWphi=asin(R/CWkyori),CCphi=asin(R/CCkyori);
        long double CWtheta=CWbtheta+CWphi,CCtheta=CCtheta=CCbtheta-CCphi;
        if(i==0) baseCWangle=nibutan(SX,SY,SX-3.0*cos(CWbtheta),SY-3.0*sin(CWbtheta)),baseCCangle=nibutan(SX,SY,SX-3.0*cos(CWbtheta),SY-3.0*sin(CWbtheta));
        long double CWangle=nibutan(CWX,CWY,CWX+3.0*cos(CWtheta),CWY+3.0*sin(CWtheta));
        long double CCangle=nibutan(CCX,CCY,CCX+3.0*cos(CCtheta),CCY+3.0*sin(CCtheta));
        if(CWangle>baseCWangle) CWangle-=M_PI*2.0;
        if(CCangle<baseCCangle) CCangle+=M_PI*2.0;
        baseCWangle=CWangle,baseCCangle=CCangle;
        CWX=cos(CWangle),CWY=sin(CWangle),CCX=cos(CCangle),CCY=sin(CCangle);
        prin(setprecision(11)<<min((long double)1.0,(CCangle-CWangle)/(M_PI*2.0)));
    }
    return 0;
}
原文