10.4 模拟赛
前言
模拟赛状态良好。
但是第一题乱写关闭同步流掉了92pts而成功爆零,第三题乱写 bitset 导致84->28。(这里剪枝比 bitset 还好)第二题与正解擦肩而过。
解题部分
T1
简要题意
给一个图,边权
- 先比较长度,在比较字典序
- 直接比较字典序
分析
按题目直接写,但是空间爆炸了(正解应该是压一下空间)
代码
#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,M=5e5+5;
int inline read()
{
int num=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){num=(num<<3)+(num<<1)+(ch^48);ch=getchar();}
return num*f;
}
int n,m;
bool flag;
struct edge{int u,v,nxt;string w;}e[M<<1];
int cnt,head[N];
void add(int u,int v,string w){e[++cnt]=(edge){u,v,head[u],w};head[u]=cnt;}
bool cmp(string x,string y)
{
if(!flag)return x.size()!=y.size()?x.size()<y.size():x<y;
else return x<y;
}
struct data
{
int id;string w;
bool operator ()(data x,data y){return !cmp(x.w,y.w);}
};
string f[N];bool vis[N];
void bfs()
{
for(int i=2;i<=n;i++)f[i]="-";
memset(vis,0,sizeof(vis));
priority_queue<data,vector<data>,data>q;
q.push((data){1,""});
while(!q.empty())
{
data d=q.top();q.pop();
int u=d.id;string s=d.w;
if(vis[u])continue;
vis[u]=1;
if(u==n){cout<<s<<" ";return;}
for(int i=head[u];i;i=e[i].nxt)
{
int v=e[i].v;string w=e[i].w;
if(vis[v])continue;
if(f[v]=="-"||cmp(s+w,f[v]))q.push((data){v,f[v]=s+w});
}
}
}
int main(){
n=read();m=read();
for(int i=1;i<=m;i++)
{
int u,v;string w;
cin>>u>>v>>w;
add(u,v,w);
}
flag=0;bfs();
flag=1;bfs();
return 0;
}
/*
4 6
1 2 aa
1 3 b
1 4 c
2 3 d
2 4 dd
3 4 c
*/
T2
简要题意
定义
给定
其中
分析(正解)
首先发现每迭代一次必定减半(除了一开始为奇数的时候的第一次),所以迭代次数
接着根据这个,我们以
下面重点讲第二个:
首先我们需要找到这个区间的左右端点,但是由于我们不好找到第一个
好的,还有一个疑问,如何求 口胡
话说我还挺久没写
代码
#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
#define ll long long
ll inline read()
{
ll num=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){num=(num<<3)+(num<<1)+(ch^48);ch=getchar();}
return num*f;
}
ll n,T,B;
ll p[N],phi[N],tot;
ll Tim[N],ans[N],s[N],cnt;
bool vis[N];
bool IsPrime(ll x)
{
if(x<2)return false;
ll i=2;
while(i*i<=x&&x%i)i++;
return i*i>x;
}
ll getphi(ll x)
{
if(x<=n)return phi[x];
ll res=x,i=1;
while(p[i]*p[i]<=x)
{
if(x%p[i]==0)
{
res-=res/p[i];
while(x%p[i]==0)x/=p[i];
}
i++;
}
if(x!=1)res-=res/x;
return res;
}
ll solve(ll x,ll tim)
{
if(tim<=0)return x;
while(tim--&&x!=1)x=getphi(x);
return x;
}
void init()
{
n=1e6;
phi[1]=1;
for(int i=2;i<=n;i++)
{
if(!vis[i])p[++tot]=i,phi[i]=i-1;
for(int j=1;i*p[j]<=n;j++)
{
vis[i*p[j]]=1;
if(i%p[j]==0){phi[i*p[j]]=phi[i]*p[j];break;}
phi[i*p[j]]=phi[i]*(p[j]-1);
}
}
for(int i=B+1;;i++)
{
cnt++;
if(!IsPrime(i))Tim[cnt]=Tim[cnt-1];
else Tim[cnt]=i-1-B;
if(Tim[cnt]>30)break;
}
cnt--;
for(int i=1;i<=cnt;i++)ans[i]=solve(i+B,Tim[i]),s[i]=s[i-1]+ans[i];
//[B+1,B+cnt]
}
ll calc(ll x)
{
if(x<=B)return (1ll+x)*x/2;
if(x<=B+cnt)return (1ll+B)*B/2 + s[x-B];
return (1ll+B)*B/2 + s[cnt] + x-(B+cnt);
}
int main(){
T=read();B=read();
init();
while(T--)
{
ll l,r;
l=read();r=read();
printf("%lld\n",calc(r)-calc(l-1));
}
return 0;
}
/*
5 4
1 3
2 4
3 5
4 6
5 7
*/
T3
简要题意
给一棵树,要求你给其赋上点权
要求
分析
一眼口胡一个中序遍历,但是发现不一定是二叉树。
但差不多,对于一个树,需要将子树分成两堆,要求一堆小于根,一堆大于根,所以贡献则为两个相乘,显然需要分成的两堆的数量尽可能平均。
然后就背包了。但是菊花图会寄。(不知道是不是正解的思路)
关于优化,垃圾 bitset 由于是固定的长度,甚至还没有剪枝好。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e6+5;
int inline read()
{
int num=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){num=(num<<3)+(num<<1)+(ch^48);ch=getchar();}
return num*f;
}
int n;
struct edge{int u,v,nxt;}e[N<<1];
int cnt,head[N];
void add(int u,int v){e[++cnt]=(edge){u,v,head[u]};head[u]=cnt;}
ll num[N],ans;
bool vis[N];
void dfs(int u,int fa)
{
num[u]=1;
for(int i=head[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa)continue;
dfs(v,u);num[u]+=num[v];
}
vis[0]=1;
for(int i=1;i<=num[u]/2;i++)vis[i]=0;
for(int i=head[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa)continue;
for(int j=num[u]/2;j>=num[v];j--)
if(vis[j-num[v]])vis[j]=1;
}
for(int i=num[u]/2;i;i--)
if(int(vis[i])){ans+=i*(num[u]-1-i);break;}
}
int main(){
n=read();
for(int i=1;i<n;i++)
{
int u=read(),v=read();
add(u,v);add(v,u);
}
dfs(1,0);
printf("%lld",ans);
return 0;
}
/*
5
1 2
2 3
1 4
4 5
6
1 2
2 3
2 4
1 5
5 6
*/
T4
T4看不懂题目。。。我不会说是我懒得看的
搞成图片放上来吧:
Input 1:
8 abababc
Insert 0 abababababababab
Search 1 16
Count 1 16 a
Replace 7 8 cabc
Search 1 18
Count 1 18 c
Insert 7 abab
Search 1 14
Output 1:
0
8
1
2
2
Input 2:
2 ababab
Insert 0 abababababababab
Search 1 16
Output 2:
6