Codeforces Round #613 (Div. 2)补题


题目

A. Mezo Playing Zoma

签到题
脑筋急转弯?

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    string s;
    scanf("%d",&n);
    cin>>s;
    cout<<n+1<<endl;
}

B. Just Eat It!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+10;
const ll INF=0x3f3f3f3f;
ll a[maxn];
ll st[maxn];
ll len[maxn];
int main(){
    ll t;
    scanf("%lld",&t);
    while(t--){
        ll n;
        ll sum=0;
        ll pos=0;
        ll l=0;
        ll MAX=-INF;
        scanf("%lld",&n);
        for(ll i=1;i<=n;i++){
            scanf("%lld",&a[i]);
            sum+=a[i];
        }
        for(ll i=1;i<=n;i++){
            if(st[i-1]>0){
                st[i]=st[i-1]+a[i];
                len[i]=len[i-1]+1;
            }
            else{
                st[i]=a[i];
                len[i]=1;
            }
            if(st[i]>MAX){
                pos=i;
                MAX=st[i];
                l=len[i];
            }
        }
        if(pos==n&&sum==MAX&&l==n)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

C. Fadi and LCM

十几行的题用了将近150行?感觉自己像个脑残

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
const ll INF=1e12+10;
ll n,prim[1000];
ll ans;
int tot;
ll mul_mod(ll a,ll b,ll c)
{
	ll t=0;
	a%=c;
	b%=c;
	while(b){
		if(b&1){
			t=(t+a)%c;
		}
		a<<=1;
		a%=c;
		b>>=1;
	}
	return t;
}
ll pow_mod(ll x,ll n,ll mod){
    if (n==1) return x%mod;
    int bit[90],k=0;
    while (n){
        bit[k++]=n&1;
        n>>=1;
    }
    ll ret=1;
    for (k=k-1;k>=0;k--){
        ret=mul_mod(ret,ret,mod);
        if (bit[k]==1) ret=mul_mod(ret,x,mod);
    }
    return ret;
}
bool check(ll a,ll n,ll x,ll t)
{
	ll ret=pow_mod(a,x,n);
	ll tmp=ret;
	for(int i=0;i<t;i++){
		ret=mul_mod(ret,ret,n);
		if(ret==1&&tmp!=1&&tmp!=n-1) return true;
		tmp=ret;
	}
	if(ret!=1) return true;
	return false;
}
bool Miller_rabin(ll n)
{
	ll x=n-1,t=0;
	while((x&1)==0){
		x>>=1;
		t++;
	}
	int ok=1;
	if(t>=1&&(x&1)){
		for(int i=0;i<20;i++){
			ll a=rand()%(n-1)+1;
			if(check(a,n,x,t)){
				ok=1;break;
			}
			ok=0;
		}
	}
	if(!ok||n==2) return false;
	return true;
}
ll gcd(ll a,ll b){
    if (a==0) return 1;
    if (a<0) return gcd(-a,b);
    while (b){
        ll t=a%b; a=b; b=t;
    }
    return a;
}
ll Pollard_rho(ll x,ll c)
{
	ll i=1,k=2;
	ll x0=rand()%x,y=x0;
	while(1){
		i++;
		x0=(mul_mod(x0,x0,x)+c)%x;
		ll d=gcd(y-x0,x);
		if(d>1&&d<x) return d;
		if(y==x0) return x;
		if(i==k){
			y=x0;
			k+=k;
		}
	}
}
void findfac(ll n)
{
	if(!Miller_rabin(n)){
		prim[tot++]=n;
		return;
	}
	ll p=n;
	while(p>=n) p=Pollard_rho(p,rand()%(n-1)+1);
	findfac(p);
	findfac(n/p);
}
unordered_map<ll,ll>mp;
vector<ll>vec;
int main()
{
	srand(time(NULL));
		scanf("%lld",&n);
		if(n==1||(!Miller_rabin(n))){
			printf("1 %lld\n",n);
		}
		else{
		tot=0;
		ll ans1=1,ans2=1;
		findfac(n);
		for(int i=0;i<tot;i++){
            if(mp[prim[i]]){
                mp[prim[i]]*=prim[i];
            }
            else{
                mp[prim[i]]=prim[i];
            }
		}
        for(auto it=mp.begin();it!=mp.end();it++){
            vec.push_back(it->second);
        }
        ll MAX=INF;
        for(int i=0;i<(1<<vec.size());i++){
            ll fac1=1,fac2=1;
            ll cur=i;
            int pos=0;
            while(cur){
                if(cur&1)
                fac1*=vec[pos];
                pos++;
                cur>>=1;
            }
        fac2=n/fac1;
        if(max(fac1,fac2)<MAX){
            MAX=max(fac1,fac2);
            ans1=fac1;
            ans2=fac2;
        }
        }
		printf("%lld %lld\n",ans1,ans2);
		}

}



D. Dr. Evil Underscores

非常经典的01字典树dp
遇到异或之类的位运算可以往二叉树的方向思考
非常经典的一道题目。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
    ll num;
    node *left,*right;
    node(){
        num=0;
        left=right=NULL;
    }
}*root;
void dfs(node* root,ll depth){
    if(root->left!=NULL){
        dfs(root->left,depth+1);
    }
    if(root->right!=NULL){
        dfs(root->right,depth+1);
    }
    if(root->right==NULL&&root->left==NULL){
        root->num=0;
    }
    else if(root->right==NULL)
        root->num=root->left->num;
    else if(root->left==NULL)
        root->num=root->right->num;
    else
        root->num=(1<<(29-depth))+min(root->left->num,root->right->num);
}
int main(){
    ll n;
    scanf("%lld",&n);
    root=new node;
    for(ll i=1;i<=n;i++){
        ll x;
        scanf("%lld",&x);
        node* cur=root;
        for(ll i=29;i>=0;i--){
            if((x>>i)&1){
                if(cur->right==NULL){
                    cur->right=new node;
                }
                cur=cur->right;
            }
            else{
                if(cur->left==NULL){
                    cur->left=new node;
                }
                cur=cur->left;
            }
        }
    }
    dfs(root,0);
    cout<<root->num<<endl;
    return 0;
}

E. Delete a Segment

先求出不删除区间的区间数为ans1,然后算去掉每一个区间造成区间总数变化的量(可能增加也可能减少),然后取最大值ans2

#include<bits/stdc++.h>
using namespace std;
const int maxn=4e5+10;
const int INF=0x3f3f3f3f;
int a[maxn];
int ans[maxn];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
    memset(ans,0,sizeof(ans));
    int n;
    scanf("%d",&n);
    vector<pair<int,int> >vec;
    for(int i=1;i<=n;i++){
        int l,r;
        scanf("%d%d",&l,&r);
        vec.push_back(make_pair(l,-i));
        vec.push_back(make_pair(r,i));
    }
    sort(vec.begin(),vec.end());
    multiset<int>se;
    int ans1=0;
    for(int i=0;i<vec.size();i++){
        if(vec[i].second<0)
            se.insert(-vec[i].second);
        else
            se.erase(se.find(vec[i].second));
        if(se.size()==0)
            ans1++;
        if(se.size()==1&&vec[i].second>0&&vec[i+1].second<0&&vec[i+1].first>vec[i].first){
            ans[*se.begin()]++;
        }
        if(se.size()==1&&vec[i].second<0&&vec[i+1].second>0)
            ans[*se.begin()]--;
    }
    int ans2=-INF;
    for(int i=1;i<=n;i++){
        ans2=max(ans2,ans[i]);
    }
    cout<<ans1+ans2<<endl;
    }
}
发布了29 篇原创文章 · 获赞 4 · 访问量 685

猜你喜欢

转载自blog.csdn.net/qq_44290978/article/details/103949396