In 2012 NOIP universal solution to a problem set rematch

The topics covered algorithm:

  • Prime factor decomposition: Getting Started;
  • Treasure Hunt: simulation;
  • Placed flowers: dynamic programming;
  • Cultural tours: search.

Prime factorization of

Topic links: https://www.luogu.org/problem/P1075
This question is only open for loop will be able to seek out the book.
An open loop variable i from 2 has been started jerk, the first encounter of n divisible i, outputs \ (n / i \) and then to break.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
    cin >> n;
    for (int i = 2; ; i ++) if (n % i == 0) { cout << n / i << endl; break; }
    return 0;
}

Treasure Hunt

Topic links: https://www.luogu.org/problem/P1076
standard simulation questions, about the process can be simulated climb of the room.
But my language is not good, This question is read for a long time to read.
You can optimize, optimize the principle is not kept looking for the removal of the cycle.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
const int MOD = 20123;
int n, m, has_key[10010][110], num[10010][110], sum[10010], id, ans;
int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i ++)
        for (int j = 0; j < m; j ++) {
            cin >> has_key[i][j] >> num[i][j];
            if (has_key[i][j]) sum[i] ++;
        }
    cin >> id;
    for (int i = 0; i < n; i ++) {
        int cnt = 0;
        int x = num[i][id] % sum[i];
        if (x == 0) x = sum[i];
        ans = (ans + num[i][id]) % MOD;
        while (true) {
            if (has_key[i][id]) {
                cnt ++;
                if (cnt == x) break;
            }
            id = (id + 1) % m;
        }
    }
    cout << ans << endl;
    return 0;
}

Placed flowers

Topic links: https://www.luogu.org/problem/P1077
This question is solved by dynamic programming.
We make state \ (f [i] [j ] \) shows a front \ (i \) flowers put a total \ (j \) number scheme basin, then:

  • \(f[0][0] = 0\)
  • \(f[i][j] = \sum_{k=0}^{\min(a[i], j)} f[i-1][k]\)

Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000007;
const int maxn = 110;
int n, m, a[maxn], f[maxn][maxn];
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    f[0][0] = 1;
    for (int i = 1; i <= n; i ++)
        for (int j = 0; j <= m; j ++)
            for (int k = 0; k <= a[i] && j-k >= 0; k ++)
                f[i][j] = (f[i][j] + f[i-1][j-k]) % MOD;
    cout << f[n][m] << endl;
    return 0;
}

cultural trip

Topic links: https://www.luogu.org/problem/P1078
built map, conditional depth-first search.
This question is then the purpose of the data may be more water, so I added a condition which is more than 1,000 the number of steps I'll just determined to not be found, then the AC embarrassing ~.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int N, K, M, S, T, d = INT_MAX, c[maxn], a[maxn][maxn], u, v, w;
bool vis[maxn];
vector< pair<int, int> > g[maxn];
void dfs(int u, int tmp) {
    if (tmp >= 1000) return;    // 没想到这样就AC了~~~
    if (tmp >= d) return;
    if (u == T) {
        d = tmp;
        return;
    }
    vis[ c[u] ] = true;
    int sz = g[u].size();
    for (int i = 0; i < sz; i ++) {
        int v = g[u][i].first;
        int w = g[u][i].second;
        if (vis[ c[v] ] ) continue;
        bool flag = true;
        for (int i = 1; i <= K; i ++) {
            if (vis[i] && a[ c[v] ][i]) {
                flag = false;
                break;
            }
        }
        if (flag) {
            dfs(v, tmp+w);
        }
    }
    vis[ c[u] ] = false;
}
int main() {
    cin >> N >> K >> M >> S >> T;
    for (int i = 1; i <= N; i ++) cin >> c[i];
    for (int i = 1; i <= K; i ++)
        for (int j = 1; j <= K; j ++)
            cin >> a[i][j];
    while (M --) {
        cin >> u >> v >> w;
        g[u].push_back( make_pair(v, w) );
        g[v].push_back( make_pair(u, w) );
    }
    dfs(S, 0);
    if (d == INT_MAX) d = -1;
    cout << d << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/codedecision/p/11741738.html