layout: post
title: 牛客小白月赛12
author: "luowentaoaa"
catalog: true
mathjax: true
tags:
- 线性筛
- 数论
- 树状数组
---
传送门
A华华听月月唱歌 (贪心)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=1e5+50;
const int inf=1e9;
typedef unsigned long long ull;
int n,m,tot;
struct node{
int l,r;
}my[maxn];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
cin>>m>>n;
for(int i=1;i<=n;i++)cin>>my[i].l>>my[i].r;
sort(my+1,my+1+n,[](node a,node b){
return a.l<b.l;
});
int l=1,k=1;int ans=0;
while(l<=m&&k<=n){
int t=0;
while(my[k].l<=l && k<=n)t=max(my[k].r,t),k++;
l=t+1;ans++;
if(t==0)k++;
}
if(l>m)cout<<ans<<endl;
else cout<<-1<<endl;
return 0;
}
B.华华教月月做数学 (快速幂+龟速乘)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=1e5+50;
const int inf=1e9;
typedef unsigned long long ull;
ll a,b,p;
ll gw(ll a,ll b){
ll ret=0;
while(b){
if(b&1)ret=(ret+a)%p;
b>>=1;
a=(a+a)%p;
}
return ret;
}
ll pw(ll a,ll b){
ll ret=1;
while(b){
if(b&1)ret=gw(ret,a);
a=gw(a,a);b>>=1;
}
return ret;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t;
cin>>t;
while(t--){
cin>>a>>b>>p;
cout<<pw(a,b)<<endl;
}
return 0;
}
C.华华给月月出题 (线性筛 极性函数)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=2e7+50;
const int inf=1e9;
typedef unsigned long long ull;
ll value[maxn];
bool check[maxn];
ll prime[maxn];
ll pw(ll a,ll n){
ll ans=1;
while(n){
if(n&1)ans=(1LL*ans*a)%mod;
a=(a*a)%mod;
n>>=1;
}
return ans;
}
ll getvalue(ll n){
value[1]=1;
int tot=0;
for(ll i=2;i<=n;i++){
if(!check[i]){
value[i]=1LL*pw(1LL*i,1LL*n);
prime[tot++]=i;
}
for(ll j=0;j<tot;j++){
if(1LL*i*prime[j]>n)break;
check[i*prime[j]]=true;
value[i*prime[j]]=1LL*value[i]*value[prime[j]]%mod;
if(i%prime[j]==0)break;
/* else{
value[i*prime[j]]=1LL*value[i]*value[prime[j]]%mod;
}*/
}
}
ll ans=0;
for(int i=1;i<=n;i++)ans=(1LL*ans^value[i]);
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
ll n;
cin>>n;
cout<<getvalue(n);
return 0;
}
D.月月给华华出题 (线性筛欧拉函数)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=1e6+50;
const int inf=1e9;
typedef unsigned long long ull;
bool check[maxn];
int phi[maxn];
int prime[maxn];
int tot;
void phi_and_prime_table(int N){
phi[1]=1;
for(int i=2;i<=N;i++){
if(!check[i]){
prime[tot++]=i;
phi[i]=i-1;
}
for(int j=0;j<tot;j++){
if(i*prime[j]>N)break;
check[i*prime[j]]=true;
if(i%prime[j]==0){
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else{
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
}
}
ll ans[maxn];
ll cal(int n){
return 1LL*n*phi[n]/2+(n==1);
}
int main()
{
/*std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);*/
int n;
scanf("%d",&n);
phi_and_prime_table(n);
for(int i=1;i<=n;i++){
for(int j=i;j<=n;j+=i)ans[j]+=cal(j/i);
}
for(int i=1;i<=n;i++)
printf("%lld\n",ans[i]);
return 0;
}
E.华华给月月准备礼物 (二分)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=2e5+50;
const int inf=1e9;
typedef unsigned long long ull;
int a[maxn];
int n,k;
bool ok(int mid){
int ans=0;
for(int i=1;i<=n;i++){
ans+=a[i]/mid;
}
if(ans>=k)return true;
else return false;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int l=1,r=1e9+1,ans=0;
while(l<=r){
int mid=(l+r)/2;
if(ok(mid)){
ans=mid,l=mid+1;
}
else r=mid-1;
}
cout<<ans<<endl;
return 0;
}
F.华华开始学信息学 (分类讨论+树状数组)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=1e5+50;
const int inf=1e9;
typedef unsigned long long ull;
ll lazy[maxn];
ll sum[maxn];
int block;
void update(int x,int val){
while(x<maxn){
sum[x]+=val;
x+=(x&(-x));
}
}
ll query(int x){
ll ans=0;
while(x){
ans+=sum[x];
x-=(x&(-x));
}
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int n,m;
cin>>n>>m;
block=sqrt(n);
int op,a,b;
while(m--){
cin>>op>>a>>b;
if(op==1){
if(a>block){
for(int i=a;i<=n;i+=a)update(i,b);
}
else lazy[a]+=b;
}
else{
ll ans=query(b)-query(a-1);
for(int i=1;i<=block;i++){
ans+=(b/i-(a-1)/i)*lazy[i];
}
cout<<ans<<endl;
}
}
return 0;
}
G.华华对月月的忠诚 (思维)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=1e5+50;
const int inf=1e9;
typedef unsigned long long ull;
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
ll a,b;
string n;
cin>>a>>b>>n;
cout<<__gcd(a,b);
return 0;
}
H.华华和月月种树 (树状数组)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=5e5+50;
const int inf=1e9;
typedef unsigned long long ull;
ll sum[maxn],cnt,sz[maxn],id[maxn];
void update(int x,int val){
while(x<maxn){
sum[x]+=val;
x+=(x&(-x));
}
}
ll query(int x){
ll ans=0;
while(x){
ans+=sum[x];
x-=(x&(-x));
}
return ans;
}
struct node{
int a,b,c;
}my[maxn];
vector<int>G[maxn];
void add(int u,int v){
G[u].push_back(v);
G[v].push_back(u);
}
void dfs(int now,int pre){
sz[now]=1;
id[now]=++cnt;
for(auto i:G[now]){
if(i==pre)continue;
dfs(i,now);
sz[now]+=sz[i];
}
}
int n;
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int m;
cin>>m;
for(int i=1;i<=m;i++){
cin>>my[i].a>>my[i].b;
if(my[i].a==1){
n++;
add(my[i].b,n);
}
if(my[i].a==2)cin>>my[i].c;
}
dfs(0,-1);
int res=0;
for(int i=1;i<=m;i++){
if(my[i].a==1){
res++;
int x=query(id[res]);
update(id[res],-x);
update(id[res]+sz[res],x);
}
else if(my[i].a==2){
update(id[my[i].b],my[i].c);
update(id[my[i].b]+sz[my[i].b],-my[i].c);
}
else cout<<query(id[my[i].b])<<endl;
}
return 0;
}
I.华华和月月逛公园 (求桥)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=1e5+50;
const int inf=1e9;
typedef unsigned long long ull;
struct Edge{
int u,v;
};
///割顶 bccno 无意义
int pre[maxn],iscut[maxn],bccno[maxn],dfs_clock,bcc_cut;
vector<int>G[maxn],bcc[maxn];
stack<Edge>S;
void init(int n){
for (int i = 0; i < n; i++) G[i].clear();
}
int lowu[maxn];
int father[maxn];
inline void add_edge(int u, int v) { G[u].push_back(v), G[v].push_back(u); }
void dfs(int u,int fa){
lowu[u] = pre[u] = dfs_clock++;
father[u]=fa;
int child = 0;
for(int i = 0; i < G[u].size(); i++){
int v =G[u][i];
if(pre[v]==-1){
dfs(v,u);
lowu[u]=min(lowu[u],lowu[v]);
}
else if(v!=fa){
lowu[u]=min(lowu[u],pre[v]);
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
add_edge(u,v);
}
memset(pre,-1,sizeof(pre));
memset(lowu,-1,sizeof(lowu));
dfs(1,-0);
int sum=0;
for(int i=1;i<=n;i++){
int v=father[i];
if(v>0&&lowu[i]>pre[v])sum++;
}
cout<<m-sum<<endl;
return 0;
}
J. 月月查华华的手机 (贪心)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=1e6+50;
const int inf=1e9;
typedef unsigned long long ull;
int go[maxn][30];
char s[maxn];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
cin>>s;
int len=strlen(s);
for(int i=0;i<26;i++){
go[len][i]=-1;
}
for(int i=len-1;i>=0;i--){
for(int j=0;j<26;j++){
if(s[i]==char('a'+j)){
go[i][j]=i+1;
}
else
go[i][j]=go[i+1][j];
}
}
/* for(int i=0;i<26;i++){
cout<<char('a'+i)<<" ";
}
cout<<endl;
for(int i=0;i<=len;i++){
for(int j=0;j<26;j++){
cout<<go[i][j]<<" ";
}
cout<<endl;
}*/
int m;
cin>>m;
while(m--){
cin>>s;
len=strlen(s);
int now=0,flag=0;
for(int i=0;i<len;i++){
now=go[now][s[i]-'a'];
if(now==-1){
flag=1;break;
}
// cout<<"i="<<i<<" now="<<now<<endl;
}
if(flag)cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
return 0;
}