1. 程式人生 > >新年第一篇!西南民族大學第十屆校賽(同步賽)

新年第一篇!西南民族大學第十屆校賽(同步賽)

https://ac.nowcoder.com/acm/contest/322#question

 

A.dreamstart的催促

程式碼:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
 
const long long mod = 10000019LL;
 
long long Pow(long long a, long long b) {
    long long ans1 = 1;
     
    a = a % mod;
     
    
while(b) { if(b % 2) { ans1 = (ans1 * a) % mod; b --; } else { a = (a * a) % mod; b /= 2; } } return ans1; } int main() { int n; scanf("%d", &n); long long ans = 0; for(int i = 1; i <= n; i ++) {
long long x; scanf("%lld", &x); ans = (ans + Pow(x, 1LL * i)) % mod; } cout << ans << endl; return 0; }
View Code

B.TRDD got lost again

程式碼:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include 
<vector> #include <queue> using namespace std; const int maxn = 6010; int n, m; char s[maxn][maxn]; int f[maxn][maxn]; int sx, sy; int ex, ey; int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; int out(int x, int y) { if(x < 0 || x > n) return 1; if(y < 0 || y > m) return 1; return 0; } int main() { scanf("%d%d", &n, &m); getchar(); n = 2 * n + 1; m = 2 * m + 1; for(int i = 0; i < n; i ++) { gets(s[i]); for(int j = 0; s[i][j]; j ++) { if(s[i][j] == 'S') s[i][j] = ' ', sx = i, sy = j; if(s[i][j] == 'T') s[i][j] = ' ', ex = i, ey = j; } } f[sx][sy] = 1; queue<int> q; q.push(sx * m + sy); while(!q.empty()) { int pr = q.front(); q.pop(); int nx = pr / m; int ny = pr % m; for(int i = 0; i < 4; i ++) { int tx = nx + dir[i][0]; int ty = ny + dir[i][1]; if(out(tx, ty)) continue; if(f[tx][ty]) continue; if(s[tx][ty] != ' ') continue; f[tx][ty] = f[nx][ny] + 1; q.push(tx * m + ty); } if(f[ex][ey]) break; } if(f[ex][ey] == 0) { printf("TRDD Got lost...TAT\n"); } else { printf("%d\n", (f[ex][ey] + 1) / 2); } return 0; }
View Code

C.Company

程式碼:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>
#include <queue>
using namespace std;
  
const int maxn = 2e5 + 10;
int n, k;
int a[maxn];
 
int v[maxn * 2], nx[maxn * 2], h[maxn], sz;
 
int ans[maxn], f[maxn];
 
void add(int U, int V) {
    v[sz] = V;
    nx[sz] = h[U];
    h[U] = sz ++;
}
 
void dfs(int x) {
    f[x] = 1;
 
    for(int i = h[x]; i != -1; i = nx[i]) {
        if(f[v[i]]) continue;
        dfs(v[i]);
        ans[x] += ans[v[i]];
    }
 
    ans[x] += a[x];
}
 
int main() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i ++) {
        int x;
        scanf("%d", &x);
        a[i] = (x <= k);
    }
    for(int i = 0; i <= n; i ++) h[i] = -1;
    int sz = 0;
    for(int i = 0; i < n - 1; i ++) {
        int U, V;
        scanf("%d%d", &U, &V);
        add(U, V);
        add(V, U);
    }
 
    dfs(1);
 
    for(int i = 1; i <= n; i ++) {
        printf("%d", ans[i]);
        if(i < n) printf(" ");
        else printf("\n");
    }
 
    return 0;
}
View Code

D.>A->B->C-

程式碼:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
 
const int maxn = 5e3 + 10;
int n;
int x[maxn];
 
int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++) {
        scanf("%d", &x[i]);
    }
     
    int ans = 0;
    for(int i = 1; i <= n; i ++) {
        int a = i;
        int b = x[i];
        int c = x[b];
        if(a == x[c]) {
            ans = 1;
            break;
        }
    }
     
    if(ans) printf("YES\n");
    else printf("NO\n");
     
    return 0;
}
View Code

E.PPY的字串

程式碼:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>
#include <queue>
using namespace std;
 
const int maxn = 1e5 + 10;
int x, n;
int a[maxn];
 
vector<int> F(vector<int> in) {
    vector<int> ans;
    queue<int> q;
 
    int L = 0, R = 0;
    while(L < in.size()) {
        if(R < in.size() && in[R] == in[L]) R ++;
        else {
            // [L, R - 1] are same
            q.push(R - L);
            q.push(in[L]);
            L = R;
        }
    }
 
    while(!q.empty()) {
        int num = q.front();
        q.pop();
         
        if(num == 0) {
            ans.push_back(0);
            continue;
        }
 
        stack<int> st;
        while(num) {
            st.push(num % 10);
            num = num / 10;
        }
        while(!st.empty()) {
            ans.push_back(st.top());
            st.pop();
        }
    }
    return ans;
}
 
 
int main() {
    scanf("%d%d", &x, &n);
     
    stack<int> st;
    while(x) {
        st.push(x % 10);
        x = x / 10;
    }
 
    vector<int> in;
    while(!st.empty()) {
        in.push_back(st.top());
        st.pop();
    }
 
    n --;
 
    while(n --) {
        in = F(in);
    }
 
    cout << in.size() << " ";
    for(int i = 0; i < in.size(); i ++) {
        cout << in[i];
    }
    cout << endl;
 
 
    return 0;
}
View Code

F.集訓隊脫單大法:這是一道只能由學姐我自己出資料的水題

程式碼:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
 
const int maxn = 1e5 + 10;
int n;
int a[maxn], p[maxn], q[maxn];
 
int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++) {
        scanf("%d", &a[i]);
    }
     
    p[1] = a[1];
    for(int i = 2; i <= n; i ++) p[i] = max(a[i], p[i - 1]);
     
    q[n] = a[n];
    for(int i = n - 1; i >= 1; i --) q[i] = max(a[i], q[i + 1]);
     
    int ans = 0;
    for(int i = 1; i <= n - 1; i ++) {
        ans = max(ans, abs(p[i] - q[i + 1]));
    }
     
    cout << ans << endl;
     
    return 0;
}
View Code

G.不想再WA了

程式碼:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
 
int T, n;
int dp[20][5];
 
int main() {
    scanf("%d", &T);
    while(T --) {
        scanf("%d", &n);
        dp[1][1] = 1;
        dp[1][2] = 1;
        dp[1][3] = 1;
        for(int i = 2; i <= n; i ++) {
            dp[i][1] = dp[i - 1][1] + dp[i - 1][2];
            dp[i][2] = dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][3];
            dp[i][3] = dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][3];
        }
        cout << dp[n][1] + dp[n][2] + dp[n][3] << endl;
    }
    return 0;
}
View Code

H.Ricky’s RealDan’s Ricky

程式碼:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>
#include <queue>
using namespace std;
 
int main() {
    int T;
    scanf("%d", &T);
    while(T --) {
        int n;
        scanf("%d", &n);
 
        int a[2] = {0, 0};
        for(int i = 1; i <= n; i ++) {
            int x;
            scanf("%d", &x);
            a[x % 2] ++;
        }
        if(n == 1 && a[0] == 1) printf("Ricky is Winner\n");
        else printf("RealDan is Winner\n");
    }
    return 0;
}
View Code

I.小A的期末作業

程式碼:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
 
int n;
 
int main() {
    scanf("%d", &n);
    int space = 0;
    for(int i = 1; i <= n; i ++) {
        for(int j = 0; j < space; j ++) printf(" ");
        for(int j = 1; j <= n; j ++) printf("*");
        printf("\n");
        space ++;
    }
    space --;
    for(int i = n + 1; i <= 2 * n - 1; i ++) {
        space --;
        for(int j = 0; j < space; j ++) printf(" ");
        for(int j = 1; j <= n; j ++) printf("*");
        printf("\n");
    }
    return 0;
}
View Code

J.怪盜基德 & 月之瞳寶石

程式碼:

K.正方體

程式碼:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
 
int T;
int p, q;
int a[5][5];
 
int main() {
    scanf("%d", &T);
    for(int cas = 0; cas < T; cas ++){
        for(int i = 0; i < 3; i ++) {
            for(int j = 0; j < 4; j ++) {
                scanf("%d", &a[i][j]);
                if(i == 0 && a[i][j]) p = a[i][j];
                if(i == 2 && a[i][j]) q = a[i][j];
            }
        }
         
        int ans = 1;
        if(p != q) ans = 0;
        if(a[1][0] != a[1][2]) ans = 0;
        if(a[1][1] != a[1][3]) ans = 0;
         
        if(ans) printf("Yes!\n");
        else printf("No!\n");
     
        if(cas % 50 == 49) printf("\n");
    }
    return 0;
}
View Code

L.簡單的分數

程式碼:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
 
struct X {
    int a, b;
    X(int aa, int bb) {
        a = aa;
        b = bb;
    }
};
 
int gcd(int a, int b) {
    if(b == 0) return a;
    return gcd(b, a % b);
}
 
X add(X x, X y) {
    X ans(0, 0);
     
    /*
      x.a    y.a      x.a * y.b + y.a * x.b
     ----- + ----- = -----------------------
      x.b    y.b            x.b * y.b
     */
     
    ans.a = x.a * y.b + y.a * x.b;
    ans.b = x.b * y.b;
     
    if(ans.a == 0) {
        return X(0, 1);
    }
     
    if(ans.b < 0) {
        ans.a = -ans.a;
        ans.b = -ans.b;
    }
     
    int g = gcd(abs(ans.a), abs(ans.b));
     
    ans.a /= g;
    ans.b /= g;
     
    return ans;
}
 
int main() {
    int T;
    scanf("%d", &T);
    while(T --) {
        int op;
        int a, b, c, d;
        scanf("%d", &op);
        scanf("%d%d%d%d", &a, &b, &c, &d);
         
        if(op == 0) c = -c;
         
        X p1(a, b);
        X p2(c, d);
         
        X ans = add(p1, p2);
        printf("%d/%d\n", ans.a, ans.b);
    }
    return 0;
}
View Code

M.HJ澆花

程式碼:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
 
const int maxn = 1e6 + 10;
int n;
int a[maxn];
int b[maxn];
 
int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++) {
        int L, R;
        scanf("%d%d", &L, &R);
        a[L] ++;
        a[R + 1] --;
    }
    for(int i = 0; i < maxn; i ++) {
        if(i > 0) a[i] = a[i] + a[i - 1];
        b[a[i]] ++;
    }
     
    for(int i = 1; i <= n; i ++) {
        printf("%d", b[i]);
        if(i < n) printf(" ");
        else printf("\n");
    }
     
    return 0;
}
View Code

 

這套題目是 1.1FH 一起刷的 新年第一套題 寫每一道題的過程都是開心的 新的一年少寫 bug 哦!