Codeforces Round #597 (Div.2)

A. Judge whether it is relatively prime;

#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        int a,b;scanf("%d%d",&a,&b);
        int kk=gcd(a,b);
        if(kk>1) printf("Infinite\n");
        else printf("Finite\n");
    }
    return 0;
}

B. Simulate according to the meaning of the question

#include<bits/stdc++.h>
using namespace std;
char s[110];
int ans[110];
void rua()
{
    memset(ans,0,sizeof(ans));
    int n;scanf("%d",&n);
    int a,b,c;scanf("%d%d%d",&a,&b,&c);
    scanf("%s",s);
    int sum=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='R' && b) ans[i]=2,b--,sum++;
        if(s[i]=='P' && c) ans[i]=3,c--,sum++;
        if(s[i]=='S' && a) ans[i]=1,a--,sum++;
    }
    if(sum<(n+1)/2) {printf("NO\n");return;}
    printf("YES\n");
    for(int i=0;i<n;i++)
        if(ans[i]==0) 
        {
            if(a) ans[i]=1,a--;
            else if(b) ans[i]=2,b--;
            else if(c) ans[i]=3,c--;
        }
    for(int i=0;i<n;i++)
    {
        if(ans[i]==1) printf("R");
        if(ans[i]==2) printf("P");
        if(ans[i]==3) printf("S");
    }
    printf("\n");
}
int main()
{
    int t;scanf("%d",&t);
    while(t--) rua();
    return 0;
}

C. Write down the number of consecutive u or n, and then the contribution to the answer satisfies the Fibonacci sequence;

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
const int mod=1e9+7;
char s[maxn];
int a[maxn];
ll refl[maxn];
ll rua()
{
    scanf("%s",s+1);
    int n=strlen(s+1);
    refl[0]=refl[1]=1;
    for(int i=2;i<=n;i++) refl[i]=(refl[i-1]+refl[i-2])%mod;
    a[0]=0;a[n+1]=0;
    for(int i=1;i<=n;i++)
    {
        if(s[i]=='u') a[i]=1;
        else if(s[i]=='n') a[i]=2;
        else if(s[i]=='m' || s[i]=='w') return 0;
        else a[i]=0;
    }
    int ff=0;
    ll num=0,ans=1;
    bool flag=false;
    for(int i=1;i<=n+1;i++)
        if(a[i]==ff) num++;
        else
        {
            if(ff!=0) ans=(ans*refl[num])%mod;
            ff=a[i];num=1;
        }
    return ans;
}
int main()
{
    printf("%lld\n",rua());
    return 0;
}

D. To build an edge from a super point to each point, the edge weight is c[i]; the edge weight is the cost of the two points and the Manhattan distance is multiplied by the Manhattan distance; then the minimum spanning tree runs while recording The answer

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int maxn=2007;
typedef long long ll;
int k[maxn],pre[maxn];
struct node{int x,y;} p[maxn];
struct node1{int u,v;ll w;} g[maxn*maxn];
bool cmp(node1 a,node1 b) {return a.w<b.w;}
vector<int> s1;
vector<node> s2;
int find(int x) {return pre[x]==x?x:pre[x]=find(pre[x]);}
int main()
{
    int n,tot=0,x;scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
    for(int i=1;i<=n;i++) scanf("%d",&x),g[++tot]={0,i,x};
    for(int i=1;i<=n;i++) scanf("%d",&k[i]);
    for(int i=0;i<=n;i++) pre[i]=i;
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
        if(i!=j) g[++tot]={i,j,1ll*(k[i]+k[j])*(abs(p[i].x-p[j].x)+abs(p[i].y-p[j].y))};
    sort(g+1,g+1+tot,cmp);
    ll ans=0;
    for(int i=1;i<=tot;i++)
    {
        int u=g[i].u,v=g[i].v;
        int fu=find(g[i].u),fv=find(g[i].v);
        if(fu==fv) continue;
        pre[fu]=fv;ans+=g[i].w;
        if(u==0) s1.pb(v);
        else s2.pb({u,v});
    }
    printf("%lld\n",ans);
    printf("%d\n",(int)s1.size());
    for(auto i:s1) printf("%d ",i);
    printf("\n%d\n",(int)s2.size());
    for(auto i:s2) printf("%d %d\n",i.x,i.y);
    return 0;
}

F. Digital DP;

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int s1[35],s2[35];
ll dp[35][2][2];
ll dfs(int pos,bool lim1,bool lim2)//是否要取上界
{
    if(pos==0) return 1;
    if(~dp[pos][lim1][lim2]) return dp[pos][lim1][lim2];
    int mx1,mx2;mx1=mx2=1;
    if(lim1) mx1=s1[pos];
    if(lim2) mx2=s2[pos];
    ll res=0;
    for(int i=0;i<=mx1;i++) for(int j=0;j<=mx2;j++)
        if((i&j)==0) res+=dfs(pos-1,lim1&&i==mx1,lim2&&j==mx2);
    return dp[pos][lim1][lim2]=res;
}
ll cal(ll a,ll b)
{
    if(a<0 || b<0) return 0;
    memset(dp,-1,sizeof(dp));
    for(int i=1;i<32;i++) s1[i]=a&1,a>>=1,s2[i]=b&1,b>>=1;
    return dfs(31,1,1);
}
ll rua()
{
    ll a,b;scanf("%lld%lld",&a,&b);
    return cal(b,b)-2*cal(a-1,b)+cal(a-1,a-1);
}
int main()
{
    int t;scanf("%d",&t);
    while(t--) printf("%lld\n",rua());
    return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_43813163/article/details/102875538