AtCoder Beginner Contest 284(A~F)

Better reading experience\color{red}{better reading experience}better reading experience



A - Sequence of Strings


Original Link

The main idea of ​​the topic :

  • Input Nstring, output in reverse order.

thought :

  • Check-in question.

code :

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);

string s[N];

void solve(){
    
    

    int n; cin >> n;
    for(int i = 0; i < n; i ++) cin >> s[i];
    for(int i = n - 1; i >= 0; i --) cout << s[i] << endl;

}

int main(){
    
    

    IOS;

    int _ = 1;

    // cin >> _;

    while(_ --){
    
    
        solve();
    }

    return 0;

}

B - Multi Test Cases


Original Link

The main idea of ​​the topic :

  • Count the odd numbers in a set of numbers.

thought :

  • Check-in question.

code :

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);

void solve(){
    
    

    int n; cin >> n;
    int cnt = 0;
    for(int i = 0; i < n; i ++){
    
    
        int x; cin >> x;
        if(x % 2 != 0) cnt ++;
    }
    cout << cnt << endl;
}

int main(){
    
    

    IOS;

    int _ = 1;

    cin >> _;

    while(_ --){
    
    
        solve();
    }

    return 0;

}

C - Count Connected Components


Original Link

The main idea of ​​the topic :

  • Given an undirected graph.
  • Find the number of connected blocks.

thought :

  • And look up.

code :

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

// const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);

const int N = 500;

int g[N]; 

int n, m;

int cnt = 0;

int find(int u){
    
    
    if(g[u] != u) g[u] = find(g[u]);
    return g[u];
}

void solve(){
    
    

    cin >> n >> m;
    for(int i = 1; i <= n; i ++) g[i] = i;  //初始化
    for(int i = 1; i <= m; i ++){
    
    
        int a, b; cin >> a >> b;
        g[find(a)] = find(b);
    }
    for(int i = 1; i <= n; i ++){
    
    
        if(g[i] == i) cnt ++;
    }
    cout << cnt << endl;

}

int main(){
    
    

    IOS;

    int _ = 1;

    // cin >> _;

    while(_ --){
    
    
        solve();
    }

    return 0;

}

D - Happy New Year 2023


Original Link

The main idea of ​​the topic :

  • Given an integer NNN
  • Guaranteed N = p 2 q N=p^2qN=p2 q, wherep , qp,qp,q are all prime numbers andp ≠ qp\ne qp=q
  • Find p , qp,q that satisfy the conditionsp,q

thought :

  • Fundamental Theorem of Arithmetic : Any one greater than 1 1The natural number NNof 1N , ifNNN is not a prime number, thenNNN can be uniquely decomposed into a product of finite prime numbersN = p 1 a 1 × p 2 a 2 ⋯ × piak N=p_1^{a_1}\times p_2^{a_2}\dots\times p_i^{a_k}N=p1a1×p2a2×piak, and at most there is only one greater than n \sqrt{n}n quality factor of .

Method one :

  • You can choose linear sieve to preprocess the prime number table, and then enumerate no more than N 3 \sqrt[3]{N} from small to large3N The prime number of can be judged.

Law 2 :

  • from i = 2 i = 2i=2 starts to enumerate factors, when the enumeration reachesN % i == 0,iii must beNNA factor of N.
  • Chapter iii is notNNPrime factorqq of Nq is the square factorqqq
  • (N / i) % i == 0When , it shows ithat is the square factor qqq , otherwise a prime factorp.

code :

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);

void solve(){
    
    
    LL x; cin >> x;
    for(LL i = 2; i < x ; i ++){
    
    
        if(x % i == 0){
    
    
            if((x / i) % i == 0) cout << i << ' ' << x / (i * i) << endl;
            else cout << (LL)sqrtl(x / i) << ' ' << i << endl;
            return ;
        }
    }
}

int main(){
    
    

    IOS;

    int _ = 1;

    cin >> _;

    while(_ --){
    
    
        solve();
    }

    return 0;

}

E - Count Simple Paths


Original Link

The main idea of ​​the topic :

  • Given a NNAn undirected graph with N vertices and $M$ edges.
  • Find from point 1 1Starting with 1 , the number of simple paths (paths with no repeated vertices) isKKK
  • The answer is min ( K , 1 × 1 0 6 ) min(K, 1\times 10^6)min ( K ,1×106)

thought :

  • Depth-first traversal of graphs.
  • Encounter a walkable path, increase the number by 1 11
  • over 1 0 6 10^6106 exit,

code :

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 2e5 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);

vector<int> g[N];
bool vis[N];

LL cnt = 0;

void dfs(int u){
    
    
    if(cnt > 1e6) return ;
    vis[u] = 1;
    cnt ++;
    for(int i = 0; i < g[u].size(); i ++){
    
    
        if(vis[g[u][i]]) continue;
        vis[g[u][i]] = 1;
        dfs(g[u][i]);
        vis[g[u][i]] = 0;
    }
}

void solve(){
    
    
    int n, m; cin >> n >> m;
    for(int i = 0; i < m; i ++){
    
    
        int a, b; cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1);
    cout << min(cnt, (LL)1000000) << endl;
}

int main(){
    
    

    IOS;

    int _ = 1;

    // cin >> _;

    while(_ --){
    
    
        solve();
    }

    return 0;

}

F - ABCBAC


Original Link

The main idea of ​​the topic :

  • It is known that a length of NNN stringSSS and an integeri ( 0 ≤ i ≤ N ) i(0\le i \le N)i(0iN)
  • Define the operation fi ( S ) f_i(S)fi( S ) The linked string is as follows:
    • S S exii of si characters.
    • S SS flip.
    • S SThe last ( N − i ) (Ni)of S(Ni ) characters.
  • If S = "abc", i = 2, then $f_i(S) = $ "abcbac".
  • Now given a certain string SSS lengthNNN and afterfi ( S ) f_i(S)fi( S ) results.
  • Find the original string SSSwaii __the value of i .

thought :

  • String hash.
  • enumeration iii , decision1 ∼ i 1 \sim i1i i + N + 1 ∼ 2 × N i + N + 1 \sim 2\times N i+N+12×N spliced ​​string andi + 1 ∼ N + ii + 1 \sim N + ii+1N+Whether the character strings after i flipping are the same can be determined.

code :

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'

typedef long long LL;
typedef unsigned long long ULL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

// const int N = 1e6 + 3;
// const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);

const ULL N = 2e6 + 9;
const int hash_cnt = 2; //哈希次数

int n;
string s;

ULL Prime[] = {
    
    1998585857ul,23333333333ul};
ULL base[] = {
    
    131, 146527, 19260817, 91815541}; // 字符集大小,进制数
ULL mod[] = {
    
    1000000007, 29123,998244353,1000000009,4294967291ull}; // 模数
ULL h1[N][hash_cnt], h2[N][hash_cnt], p[N][hash_cnt];

//初始化哈希
void initHash(ULL n, ULL cnt){
    
    
    p[0][cnt] = 1;
    for(int i = 1; i <= n; ++ i) p[i][cnt] = p[i - 1][cnt] * base[cnt] % mod[cnt];
    for(int i = 1; i <= n; ++ i) h1[i][cnt] = (h1[i - 1][cnt] * base[cnt] % mod[cnt] + s[i]) % mod[cnt]; // 正序hash
    for(int i = n; i >= 1; -- i) h2[i][cnt] = (h2[i + 1][cnt] * base[cnt] % mod[cnt] + s[i]) % mod[cnt]; // 逆序hash
}

//正序HASH
ULL getHash1(ULL id, ULL l, ULL r){
    
    
    return (h1[r][id] - h1[l - 1][id] * p[r - l + 1][id] % mod[id] + mod[id]) % mod[id];
}

//逆序HASH
ULL getHash2(ULL id, ULL l, ULL r){
    
    
    return (h2[l][id] - h2[r + 1][id] * p[r - l + 1][id] % mod[id] + mod[id]) % mod[id];
}

//判断区间正逆序是否相等,如果区间正逆序哈希值一样,则回文;
bool isRe(ULL id, ULL l,ULL r){
    
    
    return getHash1(id, l, r) == getHash2(id, l, r);
}

void solve(){
    
    

    cin >> n >> s;
    s = " " + s;
    initHash(2 * n, 0);
    for(int i = 0; i <= n; i ++ ){
    
    
        ULL sum1 = ((getHash1(0, 1, i) * p[n - i][0] % mod[0] + getHash1(0, n + i + 1, 2 * n)) % mod[0]);
        ULL sum2 = getHash2(0, i + 1, n + i);
        if(sum1 == sum2){
    
    
            string st = s.substr(i + 1, n);
            reverse(st.begin(), st.end());
            cout << st << endl;
            cout << i << endl;
            return;
        }
    }
    cout << -1 << endl;
}

int main(){
    
    

    IOS;

    int _ = 1;

    // cin >> _;

    while(_ --){
    
    
        solve();
    }

    return 0;

}

Guess you like

Origin blog.csdn.net/LYS00Q/article/details/128854746