跳到正文

个人记录

暑期集训Day1

JustRaincaot约 12 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 12 分钟

版本

  1. v1当前

Day1

score:0(悲

模拟赛题目

slove

elytra

C++
#include<bits/stdc++.h>
using namespace std;
const int Mn=2000005;
struct Edge {
	int to,next;
} w[Mn];
int cnt=0,h[Mn];
struct node {
	int a,b;
} a[Mn];
int n,m=0,p[10005],vst[10005],b[Mn],Rd[Mn],f[Mn];
void AddEdge(int x,int y) {
	w[++cnt]=(Edge) {
		y,h[x]
	};
	h[x]=cnt;
	Rd[y]++;
}
void init() {
	int i,j,x;
	for(i=2; i<=10000; i++) {
		if(!vst[i])p[++p[0]]=i;
		for(j=i; j<=10000; j+=i)vst[j]=1;
	}
	scanf("%d",&n);
	for(i=1; i<=n; i++) {
		scanf("%d",&x);
		for(j=1; j<=p[0]; j++) if(x%p[j]==0)break;
		a[i].a=p[j];
		a[i].b=x/p[j];
		if(a[i].a>a[i].b)swap(a[i].a,a[i].b);
		b[++m]=a[i].a;
		b[++m]=a[i].b;
	}
	sort(b+1,b+1+m);
	m=unique(b+1,b+1+m)-b-1;
	for(i=1; i<=n; i++) {
		a[i].a=lower_bound(b+1,b+1+m,a[i].a)-b;
		a[i].b=lower_bound(b+1,b+1+m,a[i].b)-b;
		AddEdge(a[i].a,a[i].b);
	}
}
void solve() {
	int i,j,x,y,ans=0;
	queue<int>q;
	for(i=1; i<=m; i++) {
		f[i]=-0x7fffffff/2;
		if(!Rd[i]) {
			q.push(i);
			f[i]=0;
		}
	}
	while(!q.empty()) {
		x=q.front();
		q.pop();
		for(j=h[x]; j; j=w[j].next) {
			y=w[j].to;
			Rd[y]--;
			f[y]=max(f[x]+1,f[y]);
			if(!Rd[y])q.push(y);
		}
	}
	for(i=1; i<=m; i++)ans=max(ans,f[i]);
	printf("%d",ans);
}
int main() {
	freopen("elytra.in","r",stdin);
	freopen("elytra.out","w",stdout);
	init();
	solve();
	return 0;
}

number

高精度开二次根号+二分法

C++
#include<bits/stdc++.h>
using namespace std;
#define MAXN 5100
struct hp{
    int a[MAXN+10];
    hp(){
        memset(a,0,sizeof a);
        a[0]=1;
    }
    hp(int n){
        memset(a,0,sizeof a);
        a[0]=0;
        while(n){
            a[++a[0]]=n%10;
            n/=10;
        }
        if(!a[0])
            a[0]=1;
    }
    hp(char *s){
        memset(a,0,sizeof a);
        int len=strlen(s);
        for(int i=1;i<=len;i++)
            a[i]=s[len-i]-'0';
        a[0]=len;
    }
    hp operator*(const hp &b)const{
        hp c;
        int i,j,len=a[0]+b.a[0];
        for(i=1;i<=a[0];i++)
            for(j=1;j<=b.a[0];j++)
                c.a[i+j-1]+=a[i]*b.a[j];
        for(i=1;i<len;i++){
            c.a[i+1]+=c.a[i]/10;
            c.a[i]%=10;
        }
        while(len>1&&!c.a[len])
            len--;
        c.a[0]=len;
        return c;
    }
    hp operator/(int b)const{
        hp c;
        int d=0,i,len=a[0];
        for(i=a[0];i;i--){
            d=d*10+a[i];
            c.a[i]=d/b;
            d%=b;
        }
        while(len>1&&!c.a[len])
            len--;
        c.a[0]=len;
        return c;
    }
    hp operator+(const hp &b)const{
        hp c;
        int len=max(a[0],b.a[0]),i;
        for(i=1;i<=len;i++){
            c.a[i]+=a[i]+b.a[i];
            c.a[i+1]=c.a[i]/10;
            c.a[i]%=10;
        }
        len++;
        while(len>1&&!c.a[len])
            len--;
        c.a[0]=len;
        return c;
    }
    hp operator-(const hp &b)const{
        hp c;
        int i,len=a[0];
        for(i=1;i<=len;i++){
            c.a[i]+=a[i]-b.a[i];
            if(c.a[i]<0)
                c.a[i]+=10,c.a[i+1]--;
        }
        while(len>1&&!c.a[len])
            len--;
        c.a[0]=len;
        return c;
    }
    void operator*=(const hp &x){
        *this=*this*x;
    }
    void operator/=(const int &x){
        *this=*this/x;
    }
    void operator+=(const hp &x){
        *this=*this+x;
    }
    void operator-=(const hp &x){
        *this=*this-x;
    }
    void print(){
        for(int i=a[0];i;i--)
            printf("%d",a[i]);
    }
    bool operator>(const hp&b)const{
        if(a[0]>b.a[0])
            return 1;
        if(a[0]<b.a[0])
            return 0;
        for(int i=a[0];i;i--)
            if(a[i]>b.a[i])
                return 1;
            else if(a[i]<b.a[i])
                return 0;
        return 0;
    }
    bool operator<(const hp&b)const{
        if(a[0]<b.a[0])
            return 1;
        if(a[0]>b.a[0])
            return 0;
        for(int i=a[0];i;i--)
            if(a[i]<b.a[i])
                return 1;
            else if(a[i]>b.a[i])
                return 0;
        return 0;
    }
    bool operator<=(const hp&b)const{
        return !(*this>b);
    }
    hp operator/(const hp&b)const{
        hp l(0),r(*this),mid;
        while(l<r){
            mid=(l+r+1)/2;
            if(mid*b<=*this)
                l=mid;
            else
                r=mid-1;
        }
        return l;
    }
    void operator/=(const hp&b){
        *this=*this/b;
    }
}x,l,r,mid;
char s[MAXN+10];
int main()
{
	scanf("%s",s);
	x=s;
	l=1;
	r.a[0]=x.a[0]/2+10;
	r.a[r.a[0]]=1;
	while(l<r){
		mid=(l+r+1)/2;
		if(mid*mid>x)
			r=mid-1;
		else
			l=mid;
	}
	l*=l;
	l.print();
	puts("");
}

sequence

动态规划

C++
#include<bits/stdc++.h>
using namespace std;
long long t,n,c,a[1000005],sum,ans,m;
inline long long read() {
	long long cnt=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9') {
		if (ch=='-')f=-1;
		ch=getchar();
	}
	while (ch>='0'&&ch<='9') {
		cnt=cnt*10+ch-48;
		ch=getchar();
	}
	return cnt*f;
}
int main() {
	t=read();
	while(t--) {
		n=read();
		m=0,sum=0,ans=0;
		for (int i=1; i<=n; i++) {
			a[i]=read();
			sum+=a[i];
			m=max(m+a[i],(long long) 0);
			ans=max(m,ans);
		}
		cout<<2*ans-sum<<endl;
	}
	return 0;
}

摘录与分析

快读

C++
inline long long read() {
	long long cnt=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9') {
		if (ch=='-')f=-1;
		ch=getchar();
	}
	while (ch>='0'&&ch<='9') {
		cnt=cnt*10+ch-48;
		ch=getchar();
	}
	return cnt*f;
}

最大子段和

P1115 最大子段和【模板】

动态规划

  1. 第一个数为当前子段的起点。
  2. 对于一个和为p的子段及子段后的一个数x:
    • 若 x+p<x , 从x开始记录新的子段。
    • 若 x+p>=x ,将x加入子段。
  3. 在执行上述处理的过程中实时更新当前有效序列的所有元素之和并取最大值。
  4. 取最大值即可。
C++
m=max(m+a[i],0);
ans=max(ans,m);
//m为当前的子段的和(贡献值),a[i]为子段后考虑的元素
原文