Educational Codeforces Round 81 (Rated for Div. 2)

A. Display The Number

answer

The bigger the better number of questions asked, and the answer will have hinted burst longlong,
so we have the following policies:
\ (the n-\) is even full-fill \ (1 \) , because \ (1 \) only two cells, it is worth
\ (n \) is odd, then the first mad fill \ (1 \) , and then left \ (3 \) a time to fill a \ (7 \) just fine

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef pair<int,int> PII;
#define X first
#define Y second
inline int read()
{
    int x=0,f=1;char c=getchar();
    while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
    while(isdigit(c)){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int w[10]={6,2,5,5,4,5,6,3,7,6};
int T,n; 
int main()
{
    T=read();
    while(T--)
    {
        n=read();
        if(n%2==0)
            for(int i=1;i<=n/2;i++)printf("1");
        else
        {
            printf("7");
            for(int i=1;i<n/2;i++)printf("1");
        }
        printf("\n");   
    }
    return 0;
}

B. Infinite Prefixes

answer

Essay card for a long time, think how hard to describe language solution

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef pair<int,int> PII;
#define X first
#define Y second
inline int read()
{
    int x=0,f=1;char c=getchar();
    while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
    while(isdigit(c)){x=x*10+c-'0';c=getchar();}
    return x*f;
}
const int maxn=100010;
int T,n,x,p0[maxn],p1[maxn],pre[maxn],MAX;
char s[maxn];
int main()
{
    T=read();
    while(T--)
    {
        mem(p0,0);mem(p1,0);mem(pre,0);
        n=read();x=read();
        int cnt=0;
        scanf("%s",s+1);
        for(int i=1;i<=n;i++)p0[i]=p0[i-1]+(s[i]=='0'),p1[i]=p1[i-1]+(s[i]=='1'),pre[i]=p0[i]-p1[i];
        if(x==0)cnt=1;
        if(pre[n])
        {
            for(int i=1;i<=n;i++)if((x-pre[i])%pre[n]==0 && (x-pre[i])/pre[n]>=0)cnt++;
        }
        else
        {
            for(int i=0;i<=n;i++)if(x==pre[i])cnt++;
        }
        if(!pre[n] && cnt)printf("-1\n");
        else printf("%d\n",cnt);
    }
    return 0;
}

C. Obtain The String

answer

Especially as the string match
but I did not do it, the template string pretreatment, save string matching delays.
Set \ (pre [i] \) represents the letter \ (I \) is the earliest position, \ (SUF [I] [J] \) represents shown in section \ (I \) after characters, letters \ (J \ ) earliest position, this can be \ (O (26n) \) pretreated out
and then jump directly to repeatedly cross at the top around it

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef pair<int,int> PII;
#define X first
#define Y second
inline int read()
{
    int x=0,f=1;char c=getchar();
    while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
    while(isdigit(c)){x=x*10+c-'0';c=getchar();}
    return x*f;
}
const int maxl=100010;
int T;
char s[maxl],t[maxl];
int pre[30],suf[maxl][30];//pre(i)表示字符i最早出现位置 ,suf(i,j)表示在i字符后,字符j最早出现位置 
int main()
{
    T=read();
    while(T--)
    {
        mem(pre,42);mem(suf,42);
        scanf("%s%s",s,t);
        int l1=strlen(s),l2=strlen(t),cnt=0,ok=0;
        for(int i=0;i<l1;i++)pre[s[i]-'a']=min(pre[s[i]-'a'],i);
        for(int i=0;i<l1-1;i++)suf[i][s[i+1]-'a']=i+1;
        for(int i=l1-1;i>=0;i--)
            for(int j=0;j<26;j++)suf[i][j]=min(suf[i][j],suf[i+1][j]);
        for(int i=0;i<l2;)
        {
            ++cnt;
            int pos=pre[t[i]-'a'];i++;
            if(pos>=l1){ok=1;break;}
            while(suf[pos][t[i]-'a']<l1)pos=suf[pos][t[i]-'a'],i++;
        }
        if(!ok)printf("%d\n",cnt);
        else printf("%d\n",-1);
    }
    return 0;
}

D. Same GCDs

answer

Set \ (GCD (A, m) = X \) , there are \ (a = k_1x, m =
k_2x \) may know \ (GCD (A + B, m) = X \) , there are \ (a + b = k_3x \)
the question now becomes, in the closed interval \ ([k_1, k_1 + k_2-1 ] \) the number of inner (K_2 \) \ prime
split into prefix problem, \ (n-\) after the decomposition of the quality factor to the principle of inclusion and exclusion factors
have a similar problem this is the link

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef pair<int,int> PII;
#define X first
#define Y second
inline LL read()
{
    LL x=0,f=1;char c=getchar();
    while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
    while(isdigit(c)){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int T;
LL a,m,x,k1,k2;
LL gcd(LL a,LL b){return b==0 ? a : gcd(b,a%b);}
#include<vector> 
vector<LL> pme;
LL count_prime(LL x,LL n){
    pme.clear();
    LL i,j;
    for(i=2;i*i<=n;i++)
        if(n%i==0){
            pme.push_back(i);
            while(n%i==0)n/=i;
        }
    if(n>1)pme.push_back(n);
    LL sum=0,value,cnt;
    for(i=1;i<(1<<pme.size());i++){
        value=1;
        cnt=0;
        for(j=0;j<pme.size();j++){
            if(i&(1<<j)){
                value*=pme[j];
                cnt++;
            }
        }
        if(cnt&1)
            sum+=x/value;
        else sum-=x/value;
    }
    return x-sum;
}
int main()
{
    T=(int)read();
    while(T--)
    {
        a=read();m=read();
        x=gcd(a,m);
        k1=a/x;k2=m/x;
        cout<<count_prime(k2+k1-1,k2)-count_prime(k1-1,k2)<<endl;
    }
    return 0;
}

nonsense

The brain or not, deal with the problem is too slow ah

Guess you like

Origin www.cnblogs.com/FYH-SSGSS/p/12241935.html