# 【CometOJ】CometOJ#8 解题报告

### $A$：杀手皇后（点此看题面）

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
using namespace std;
int n;string s;
int main()
{
RI i;string st;for(cin>>n>>s,i=2;i<=n;++i) cin>>st,st<s&&(s=st,0);//读入，求字典序最小字符串
return cout<<s<<endl,0;//输出
}

### $B$：支援城市（点此看题面）

$\sum_{i=1}^n(a_i^2-2a_ia_x+a_x^2)=\sum_{i=1}^na_i^2-2a_x\sum_{i=1}^na_i+na_x^2$

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 100000
#define LL long long
using namespace std;
int n,a[N+5];
int main()
{
RI i;LL s=0,s2=0;for(scanf("%d",&n),i=1;i<=n;++i) scanf("%d",a+i),s+=a[i],s2+=1LL*a[i]*a[i];//预处理和及平方和
for(i=1;i<=n;++i) printf("%lld ",s2-2*s*a[i]+1LL*n*a[i]*a[i]);return 0;//计算答案并输出
}

### $C$：符文能量（点此看题面）

$f_i=min(f_{i-1}+b_{i-1}a_i(k^2-1),b_{i-1}a_i(k-1))$

$Mn=min(Mn,f_{i-1}+b_{i-1}a_i(k-1))$

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 100000
#define LL long long
#define min(x,y) ((x)<(y)?(x):(y))
#define Gmin(x,y) (x>(y)&&(x=(y)))
using namespace std;
int n,k,a[N+5],b[N+5];LL f[N+5];
int main()
{
RI i;LL Mn=0,ans=0;for(scanf("%d%d",&n,&k),i=1;i<=n;++i) scanf("%d%d",a+i,b+i);//读入
for(i=1;i<=n+1;++i) Gmin(Mn,f[i-1]+b[i-1]*a[i]*(k-1)),//更新最小变化值
ans+=b[i-1]*a[i],f[i]=min(b[i-1]*a[i]*(k-1),f[i-1]+b[i-1]*a[i]*(k*k-1));//计算原先答案，并动态规划转移f[i]
return printf("%lld",ans+Mn),0;//输出答案
}

### $D$：菜菜种菜（点此看题面）

$l_i<L\le i\le R<r_i$

$l_i+1\le L\le i\le R\le r_i-1$

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 1000000
#define swap(x,y) (x^=y^=x^=y)
#define Gmin(x,y) (x>(y)&&(x=(y)))
#define Gmax(x,y) (x<(y)&&(x=(y)))
using namespace std;
int n,m,Qt,a[N+5],l[N+5],r[N+5],L[N+5],R[N+5];
vector<int> s[N+5],q[N+5];vector<int>::iterator it;
class FastIO
{
private:
#define FS 100000
#define tn (x<<3)+(x<<1)
#define D isdigit(c=tc())
char c,*A,*B,FI[FS];
public:
I FastIO() {A=B=FI;}
Tp I void read(Ty& x) {x=0;W(!D);W(x=tn+(c&15),D);}
}F;
class TreeArray//树状数组
{
private:
#define lowbit(x) (x&-x)
int v[N+5];
I void Upt(RI x,CI y) {W(x<=n) v[x]+=y,x+=lowbit(x);}//修改后缀
public:
I void Add(CI l,CI r,CI v) {Upt(l,v),Upt(r+1,-v);}//区间修改，差分
I int Qry(RI x,RI t=0) {W(x) t+=v[x],x-=lowbit(x);return t;}//单点询问
}T;
int main()
{
RI i,x,y;long long ans=0;
for(i=1;i<=n;++i) s[l[i]].push_back(i);//对于l[i]开桶，方便后面的操作
for(i=1;i<=n;++i)//枚举L
{
for(it=q[i].begin();it!=q[i].end();++it) ans^=1LL*(*it)*T.Qry(R[*it]);//枚举R询问
}return printf("%lld",ans),0;
}

### $E$：神奇函数（点此看题面）

$\sum_{i=1}^{\sqrt n}i\cdot g(\lfloor\frac n{i^2}\rfloor)$

$g(x)=\sum_{i=1}^{\sqrt x}\mu(i)\lfloor\frac n{i^2}\rfloor$

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define LS 10000000
#define LL long long
using namespace std;
LL n;
template<int SZ> class LinearSieve//线性筛
{
private:
int Pt,P[SZ+5],mu[SZ+5];
public:
LL smu[SZ+5],smu2[SZ+5];
I LinearSieve()
{
RI i,j;for(mu[1]=1,i=2;i<=LS;++i)
for(!P[i]&&(mu[P[++Pt]=i]=-1),j=1;j<=Pt&&1LL*i*P[j]<=SZ;++j)
if(P[i*P[j]]=1,i%P[j]) mu[i*P[j]]=-mu[i];else break;
for(i=1;i<=LS;++i) smu[i]=smu[i-1]+mu[i],smu2[i]=smu2[i-1]+mu[i]*mu[i];//筛mu和以及平方和
}
};LinearSieve<LS> L;
I LL G(Con LL& x)
{
if(x<=LS) return L.smu2[x];RI l,r,sx=sqrt(x);LL res=0;//x较小时直接返回答案
for(l=1;l<=sx;l=r+1) r=sqrt(x/(x/(1LL*l*l))),res+=x/(1LL*l*l)*(L.smu[r]-L.smu[l-1]);//除法分块
return res;//返回答案
}
int main()
{
RI Tt,l,r,sn;LL ans=0;scanf("%d",&Tt);W(Tt--)
{
scanf("%lld",&n),sn=sqrt(n),ans=0;//读入
for(l=1;l<=sn;l=r+1) r=sqrt(n/(n/(1LL*l*l))),ans+=G(n/(1LL*l*l))*(l+r)*(r-l+1)>>1;//除法分块
printf("%lld\n",ans);//输出答案
}return 0;
}

### $F$：黄金体验（占坑待填）

posted @ 2019-08-14 20:41  TheLostWeak  阅读(...)  评论(... 编辑 收藏