Codeforces Round #506 (Div. 3) D-F

Codeforces Round #506 (Div. 3) (中等难度)

自己的做题速度大概只尝试了D题,不过TLE

D. Concatenated Multiples

题意

  • 数组a[],长度n,给一个数k,求满足条件的(i,j)(i!=j) a[i],a[j]连起来就可以整除k
  • 连起来的意思是 20,5连起来时205; 5,20连起来时520
  • n<=2*1e5,k<=1e9,ai<=1e9
  • 愚蠢的思路是像我一样遍历(i,j)可能性,然后TLE,因为这是O(n^2)
  • 可以先思考一简单问题如何不是连起来而是a[i]+a[j]如何计算,很简单将a[]%k进行计数,然后计数(k-a[i])%k,这时查找只需O(logn)
  • 这里a[i]*(10^k),k实际上只有10中可能所以也可以存起来
  • 时间复杂度O(nlogn)

    代码

#include<bits/stdc++.h>
const int maxn=200020;

#define ll long long
int a[maxn];
int mod[maxn];
int ka[maxn];
int n,k;

int geth(int x){
    ll mul=1;
    int t=1;
    while(mul<=x){
        mul=mul*10;
        t=(t*(10%k))%k;
    }
    return t;
}
int main(){
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        mod[i]=a[i]%k;
        ka[i]=(geth(a[i]))%k;
    }
    ll ans=0;
    for(int i=1;i<=n;i++){//O(n^2)
        for(int j=1;j<=n;j++){
            if(i!=j&&((mod[i]*(ka[j]))%k+mod[j])%k==0){
                    //printf("db %d %d\n",i,j);
                    ans++;
            }

        }
    }
    printf("%I64d\n",ans);
}

E. Tree with Small Distances

题意

  • 这道题还没想出来,据说使用贪心,下面是别人代码(感觉大佬的思路都是一样,应该是触摸到问题本质)

    代码

#include<bits/stdc++.h>
using namespace std;

int n;
const int maxn = 2e5+100;
vector<int> G[maxn];
int used[maxn];
int d[maxn];
int ans = 0;

void dfs(int u,int f){
    //cout<<"enter"<<u<<" "<<f<<endl;
    bool flag = false;
    for(int i=0;i<G[u].size();i++){
        int v = G[u][i];
        if(v!=f){
            d[v] = d[u] + 1;
            dfs(v,u);
            flag|=used[v];
        }
    }
    if(d[u]>2&&!used[u]&&!flag&&!used[f]){
        used[f] = true;
        //cout<<"db"<<f<<endl;
        ans++;
    }
    //cout<<"out"<<endl;
}

int main(){
    int t;
    //freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=0;i<n-1;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        G[a].push_back(b);
        G[b].push_back(a);
    }
    dfs(1,0);
    /*
    cout<<"db1\n";
    for(int i=1;i<=n;i++){
        cout<<d[i]<<"\t";
    }
    cout<<endl;
    for(int i=1;i<=n;i++){
        cout<<used[i]<<"\t";
    }
    cout<<"db over"<<endl;
    */
    printf("%d\n",ans);
    //fclose(stdin);
    return 0;
}

F. Multicolored Markers

题意

  • 自己看
  • 枚举较短边,考虑蓝方块构成矩形的最低高度和红方块构成矩形的最低高度是否和当前高度冲突

    代码

#include<bits/stdc++.h>
#define ll long long

int main(){
    ll a,b;
    std::cin>>a>>b;
    ll c=a+b;
    ll tmp=std::max(a,b);
    ll minl=(a+b+1)*2;
    ll lowa=a;
    ll lowb=b;
    for(ll i=1;i*i<=(a+b);i++){
        if(a%i==0) lowa=a/i;
        if(b%i==0) lowb=b/i;
        if(c%i==0){
            if(c/i>=lowa||c/i>=lowb){
                //std::cout<<"db"<<i<<std::endl;
                minl=std::min(2*(i+c/i),minl);
            }
        }
    }
    std::cout<<minl<<std::endl;
}

加油,需要补题Kmp,数论+树

原文地址:https://www.cnblogs.com/fridayfang/p/9536123.html

时间: 2024-07-30 03:42:31

Codeforces Round #506 (Div. 3) D-F的相关文章

Codeforces Round #506 (Div. 3) 题解

Codeforces Round #506 (Div. 3) 题目总链接:https://codeforces.com/contest/1029 A. Many Equal Substrings 题意: 给出长度为n的字符串,然后要求你添加一些字符,使得有k个这样的字符串. 题解: 直接暴力吧...一个指针从1开始,另一个从2开始,逐一比较看是否相同:如果不同,第一个指针继续回到1,第二个指针从3开始...就这么一直重复.最后如果第二个指针能够顺利到最后一位,那么记录当前的第一个指针,把他后面的

Codeforces Round #541 (Div. 2) (A~F)

目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路) F.Asya And Kittens(链表) G.Most Dangerous Shark Codeforces 1131 比赛链接 hack一个暴力失败了两次最后还是没成功身败名裂= = CF跑的也太快了吧... 不过倒也涨了不少. A.Sea Battle //想麻烦了,但是无所谓... #

Codeforces Round #496 (Div. 3)A~F

(寒假训练赛,也是lj难得补完的一场 https://codeforces.com/contest/1005 A.题意是  每一个楼梯有x个台阶,小女孩爬楼的时候会从1开始数每层楼有几个台阶,现在给给出n个数字a1~an,代表着小女孩爬楼时数数的序列,求有多少层楼梯,并且输出每层楼梯台阶数. 解法:for循环模拟小女孩从1开始数,数到下一个1的话楼层++,然后把他前一个数存进去. int now = 0; for(int i = 0;i < n;++i){ cin>>a; if(a ==

Codeforces Round #506 (Div. 3) C. Maximal Intersection (枚举)

[题目描述] You are given $n$ segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide. The intersection of

Codeforces Round #506 (Div. 3)ABCDEF

并没有参加比赛,全是赛后AC A题 题意:现有一个长度为n的字符串s.你需要构建一个长度最小的字符串t,使得t中恰好包含k个s(允许部分重叠),输出这个字符串 1 /* 2 我们可以非常容易的发现我们构造出来的字符串t有着鲜明的特征.即t中有大量重复的子串,可以证明的是t = S + (K-1)*A, A是S串中删去一段字符串(这段字符串满足既是S的前缀又是S的后缀). 3 */ 4 #include<cstdio> 5 #include<iostream> 6 7 using n

Codeforces Round #516 (Div. 2) (A~F)

目录 A.Make a triangle! B.Equations of Mathematical Magic C.Oh Those Palindromes D.Labyrinth(BFS) E.Dwarves,Hats and Extrasensory Abilities(交互 二分) D.Labyrinth E.Dwarves,Hats and Extrasensory Abilities 比赛链接 A.Make a triangle! 不放了. B.Equations of Mathema

Codeforces Round #506 (Div. 3) C. Maximal Intersection

C. Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You are given nn segments on a number line; each endpoint of every segment has integer coordinates. Some segments ca

Codeforces Round #506 (Div. 3) A-C

CF比赛题解(简单题) 简单题是指自己在比赛期间做出来了 A. Many Equal Substrings 题意 给个字符串t,构造一个字符串s,使得s中t出现k次;s的长度最短 如t="cat",k=3, minlen(s)=9,s=catcatcat 1<=len(t),k<=50 题解 稍加思考是个前缀等于后缀的问题,用kmp的next数组模板 假设t[0..l-1]==t[n-l....n-1] 那么应该不断重复t[l...n-1]这样的子串 代码如下(应该好好记住

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/E Description Polycarp lives on a coordinate line at the point x=0. He goes to his friend that lives at the point x=a. Polycarp can