AtCoder Beginner Contest 177 summary

I played AtCoder for the first time when I was back to school, and I felt that I was not in the dormitory at night~~

A - Don’t be late

Math problem sign in

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100010;
ll a[N];
int n;
int main()
{
    
    
    IO;
    int d,t,s;
    cin>>d>>t>>s;
    int x=(d+s-1)/s;
    if(t>=x) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}

B - Substring

Direct violence

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define mkp(a,b) make_pair(a,b)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int main()
{
    
    
    IO;
    string s,t;
    cin>>s>>t;
    int res=1e9;
    for(int i=0;i+t.size()<=s.size();i++)
    {
    
    
        int cnt=0;
        for(int j=0;j<t.size();j++) 
            if(s[j+i]!=t[j]) cnt++;
        res=min(cnt,res);
    }
    cout<<res<<endl;
    return 0;
}

C - Sum of product of pairs

Preprocessing prefix and can

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=200010,mod=1e9+7;
ll a[N],s[N];
int n;
int main()
{
    
    
    IO;
    cin>>n;
    for(int i=1;i<=n;i++) 
    {
    
    
        cin>>a[i];
        s[i]=s[i-1]+a[i];
    }
    ll res=0;
    for(int i=1;i<=n;i++)
        res=(res+(s[n]-s[i])%mod*a[i]%mod)%mod;
    cout<<res<<endl;
    return 0;
}

D - Friends

And check the collection and maintenance to sz[]find the largest one

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=200010,mod=1e9+7;
int p[N],sz[N];
int n,m;
int find(int x)
{
    
    
    if(x!=p[x]) p[x]=find(p[x]);
    return p[x];
}
int main()
{
    
    
    IO;
    cin>>n>>m;
    for(int i=1;i<=n;i++) p[i]=i,sz[i]=1;
    while(m--)
    {
    
    
        int a,b;
        cin>>a>>b;
        int pa=find(a),pb=find(b);
        if(pa!=pb)
        {
    
    
            p[pa]=pb;
            sz[pb]+=sz[pa];
        }
    }
    int res=0;
    for(int i=1;i<=n;i++)   
        if(i==p[i]) res=max(res,sz[i]);
    cout<<res<<endl;
    return 0;
}

E - Cover me

It’s been a long time since I’ve done a number theory problem, and I’ve forgotten about the sieve method. I’ll go back and review kuangbin.
Method 1
If the greatest common divisor of two numbers is not 1, then the two numbers must have the same prime factors after decomposing them. Quality factor. —— O (nai) O(n\sqrt{a_i})O ( nai ) 1040ms

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1000010;
int a[N],cnt[N];
int n;
int gcd(int a,int b)
{
    
    
    return b?gcd(b,a%b):a;
}
void divide(int x)
{
    
    
    for(int i=2;i<=x/i;i++)
    {
    
    
        if(x%i==0) 
        {
    
    
            cnt[i]++;
            while(x%i==0) x/=i;
        }
    }
    if(x>1) cnt[x]++;
}
int main()
{
    
    
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) divide(a[i]);
    int d=a[1];
    for(int i=2;i<=n;i++) d=gcd(d,a[i]);
    bool ok=1;
    for(int i=1;i<N;i++)
        if(cnt[i]>1) 
        {
    
    
            ok=0;
            break;
        }
    if(ok) cout<<"pairwise coprime"<<endl;
    else if(d==1) cout<<"setwise coprime"<<endl;
    else cout<<"not coprime"<<endl;
    return 0;
}

Method two,
method one, do the reverse, decompose the original number into prime factors, method two is to do it, consider that the greatest common divisor of two numbers is d, then the two numbers must be multiples of d, directly violently sieve a number in the original array The number of multiples. If the number is greater than 1, it is not pairwise coprime- O (N log N) O(NlogN)O(NlogN) 248ms

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1000010;
int a[N],mp[N];
int n;
int gcd(int a,int b)
{
    
    
    return b?gcd(b,a%b):a;
}
int main()
{
    
    
    cin>>n;
    for(int i=1;i<=n;i++) 
    {
    
    
        cin>>a[i];
        mp[a[i]]++;
    }
    bool ok=1;
    for(int i=2;i<N;i++)
    {
    
    
        int cnt=0;
        for(int j=i;j<N;j+=i) cnt+=mp[j];
        if(cnt>1)
        {
    
    
            ok=0;
            break;
        }
    }
    int d=a[1];
    for(int i=2;i<=n;i++) d=gcd(d,a[i]);
    if(ok) cout<<"pairwise coprime"<<endl;
    else if(d==1) cout<<"setwise coprime"<<endl;
    else cout<<"not coprime"<<endl;
    return 0;
}

F - I hate Shortest Path Problem

Leave a hole in the line tree to make it up?
Come on~

Guess you like

Origin blog.csdn.net/Fighting_Peter/article/details/108316043