2021 Niu Guest Winter Holiday Algorithm Basic Training Camp 2

  Caiji autistic field  

H. Niuniu and chessboard

Sign in successfully

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const ll N = 8e7 + 7;
const ll inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= n; ++j) {
            if(i & 1) {
                if(j & 1) printf("0");
                else printf("1");
            }
            else {
                if(j & 1) printf("1");
                else printf("0");
            }
        }
        printf("\n");
    }
	return 0;
}

F. Niu Niu and Exchange Sorting

 Emm stack simulation, remove the beginning and end of the order, such as (1 2 3) 6 5 4 (7 8 9), only process the middle interval, find that the next number should be 4, 4 must be reversed to 3 at once Back, so k should be 3. After knowing k, the simulation can be done. Each round of continuous throwing k numbers into the stack, pop from the top of the stack, see if the number popped out is the number corresponding to the sequence, if not, stop the pop and reverse the stack (use Deque is more convenient. Use vector's reverse to simulate stack reversal during the game), and then the next round of push. Finally, if the stack is empty, it can be arranged in order (1, 2, 3, ..., n), which is yes, otherwise no.

Note that at the beginning of a new round of push, if the stack is empty and the current a[i] is exactly the number you are looking for, just skip it, don’t push, such as the 6 in 1 2 5 4 3 6 9 8 7 10, it should Skip directly.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;

int a[N];

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    int be = n + 1, ed = 0;
    for(int i = 1; i <= n; ++i) {
        if(a[i] != i) {
            be = i;
            break;
        }
    }
    for(int i = n; i >= 1; --i) {
        if(a[i] != i) {
            ed = i;
            break;
        }
    }
    int k = 0;
    for(int i = be + 1; i <= ed; ++i) {
        if(a[i] == be) {
            k = i - be + 1;
            break;
        }
    }
    if(!k) {
        printf("yes\n%d\n", 1);
        return 0;
    }
    vector<int>st;
    for(int i = be; i <= ed; ++i) {
        if(st.empty() && a[i] == be) {
            be++;
            continue;
        }
        if(st.size() < k) st.emplace_back(a[i]);
        if(st.size() == k) {
            while(!st.empty() && st.back() == be) st.pop_back(), be++;
            reverse(st.begin(), st.end());
        }
    }
    if(!st.empty()) printf("no\n");
    else printf("yes\n%d\n", k);
    return 0;
}
/*
7
1 6 3 2 5 4 7
8
1 4 7 2 3 6 5 8
10
1 2 5 4 3 6 9 8 7 10
*/

I. Niuniu’s "prime factor"

Stuck on this question all afternoon, you must have a long memory

I wrote several simulations during the game. T Fei was also expected, but I didn’t know where it could be optimized. Later I read the code of the big guy and realized that an answer to the number x can be transferred from x / pri, and pri is The smallest prime factor of x, so by recording the smallest prime factor of each number in the linear sieve, the prime factor decomposition can be achieved linearly, and the rest can be simulated.

Two ways of writing are temporarily supplemented, one is accumulative by multiplying the base number by the number of digits, and the other is no-brain string simulation, dp will be supplemented later

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const int N = 4e6 + 7;

ll ans, base[105];
int pri[N], tot, fac[N], n, lg[N];
bool vis[N];

void init() {
    base[0] = 1;
    for(int i = 1; i < 105; ++i) base[i] = base[i - 1] * 10 % mod;
    for(int i = 1; i < N; ++i) lg[i] = lg[i / 10] + 1;
    tot = 0;
    for(int i = 2; i < N; ++i) {
        if(!vis[i]) {
            pri[++tot] = i;
            fac[i] = i;
        }
        for(int j = 1; j <= tot && i * pri[j] < N; ++j) {
            vis[i * pri[j]] = 1;
            fac[i * pri[j]] = pri[j];
            if(i % pri[j] == 0) break;
        }
    }
}

void solve() {
    for(int i = 2; i <= n; ++i) {
        ll res = 0;
        int tmp = i;
        while(tmp > 1) {
            res = (res * base[lg[fac[tmp]]] % mod + fac[tmp]) % mod;
            tmp /= fac[tmp];
        }
        ans = (ans + res) % mod;
    }
}

int main() {
    init();
    scanf("%d", &n);
    ans = 0, solve();
    printf("%lld\n", ans);
	return 0;
}
//string模拟 不推荐
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const int N = 4e6 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

ll ans;
int pri[N], tot, fa[N], fa_pri[N], n;
bool vis[N];
string str[N];

string tostring(int x) {
    string s = "";
    while(x) {
        s = char(x % 10 + '0') + s;
        x /= 10;
    }
    return s;
}

ll tonumber(string s) {
    ll res = 0, base = 1;
    int siz = s.size();
    for(int i = siz - 1; i >= 0; --i) {
        res = (res + base * (s[i] - '0') % mod) % mod;
        base = base * 10 % mod;
    }
    return res;
}

void prime() {
    tot = 0;
    for(int i = 2; i <= n; ++i) {
        if(!vis[i]) {
            pri[++tot] = i;
            fa[i] = 1;
            fa_pri[i] = i;
        }
        for(int j = 1; j <= tot; ++j) {
            if(i * pri[j] > n) break;
            vis[i * pri[j]] = 1;
            fa[i * pri[j]] = i;
            fa_pri[i * pri[j]] = pri[j];
            if(i % pri[j] == 0) break;
        }
    }
}

void solve() {
    for(int i = 2; i <= n; ++i) {
        str[i] = "";
        int tmp = i;
        while(tmp > 1) {
            str[i] += tostring(fa_pri[tmp]);
            tmp = fa[tmp];
            if(str[tmp] != "") {
                str[i] += str[tmp];
                break;
            }
        }
        ans = (ans + tonumber(str[i])) % mod;
    }
}

int main() {
    scanf("%d", &n);
    prime();
    ans = 0, solve();
    printf("%lld\n", ans);
	return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43871207/article/details/113642419