1. 程式人生 > >9.16 騰訊筆試程式設計題ac程式碼

9.16 騰訊筆試程式設計題ac程式碼

思路: 求lcm(1, 2, …, n, …, m) = lcm(n + 1, …, m)
根據算數基本定理

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue> #include <stack> #include <map> #include <set> #include <utility> #include <bitset> // #include <unistd.h> #include <unordered_map> using namespace std; #define LL long long #define pb push_back #define mk make_pair #define pill pair<int, int>
#define fi first #define se second #define mst(a, b) memset(a, b, sizeof a) #define lson (rt << 1) #define rson ((rt << 1) | 1) const int qq = 1e6 + 10; const int INF = 1e9 + 10; const int MOD = 1e9 + 7; LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); } LL lcm(LL a, LL b) { return
a / gcd(a, b) * b; } int n; int vis[qq]; int main() { #ifdef ONLINE_JUDGE #else freopen("in.txt", "r", stdin); #endif scanf("%d", &n); if (n == 1) { printf("2\n"); return 0; } mst(vis, 0); for (int i = 2; i <= n; ++i) { int x = i; for (int j = 2; j * j <= x; ++j) { if (x % j == 0) { int cnt = 1; while (x % j == 0) { cnt *= j; x /= j; } vis[j] = max(vis[j], cnt); } } if (x > 1) { vis[x] = max(vis[x], x); } } int ans = -INF; for (int i = 2; i <= n; ++i) { if (!vis[i]) continue; int x = vis[i]; for (int j = 2; ; j++) { if (x * j > n) { x = x * j; break; } } ans = max(ans, x); } printf("%d\n", ans); return 0; }

思路: dfs每一個點走一遍

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
// #include <unistd.h>
#include <unordered_map>

using namespace std;

#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define fi first
#define se second
#define mst(a, b) memset(a, b, sizeof a)
#define lson (rt << 1)
#define rson ((rt << 1) | 1)

const int qq = 1e3 + 10;
const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
vector<int> vt[qq];
int n, m;
bool vis[qq];
int to[qq], from[qq];
void dfs(int u) {
    for (int i = 0; i < vt[u].size(); ++i) {
        int v = vt[u][i];
        if (!vis[v]) {
            vis[v] = true;
            from[v]++;
            dfs(v);
        }
    }
}
map<pill, bool> mp;

int main() {
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt", "r", stdin);
    #endif
    mst(to, 0);
    mst(from, 0);
    scanf("%d%d", &n, &m);
    for (int i = 0, a, b; i < m; ++i) {
        scanf("%d%d", &a, &b);
        if (a == b) continue;
        if (mp.find(mk(a, b)) == mp.end()) {
            vt[a].pb(b);
            mp[mk(a, b)] = true;
        }
    }
    for (int i = 1; i <= n; ++i) {
        mst(vis, false);
        vis[i] = true;
        dfs(i);
        int cnt = 0;
        for (int j = 1; j <= n; ++j) {
            if (vis[j]) cnt++;
        }
        to[i] = cnt - 1;
    }
    int cnt = 0;
    for (int i = 1; i <= n; ++i) {
        if (to[i] < from[i]) {
            cnt++;
        }
    }
    printf("%d\n", cnt);
    return 0;
}

思路: 這題怎麼做都可以
A%B, 2A%B, 3A%B …. 因為B <= 100 , 根據抽屜原理, 再加上A的倍數關係, 這裡一定會有迴圈節, 剩下的就是求一下揹包, 看看成組合哪些數

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
// #include <unistd.h>
#include <unordered_map>

using namespace std;

#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define fi first
#define se second
#define mst(a, b) memset(a, b, sizeof a)
#define lson (rt << 1)
#define rson ((rt << 1) | 1)

const int qq = 1e5 + 10;
const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
map<int, bool> mp;
vector<int> vt;
vector<int> dp;
bool vis[105];

int main() {
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt", "r", stdin);
    #endif
    int t;  scanf("%d", &t);
    int a, b, c;
    while (t--) {
        mp.clear();
        dp.clear();
        vt.clear();
        scanf("%d%d%d", &a, &b, &c);
        for (int i = 1; ; ++i) {
            if (mp.find((a * i) % b) != mp.end()) {
                break;
            }
            mp[(a * i) % b] = true;
            vt.pb((a * i) % b);
        }
        dp.pb(0);
        mst(vis, false);
        bool f = false;
        for (int i = 0; i < vt.size() && !f; ++i) {
            int len = dp.size();
            for (int j = 0; j < len && !f; ++j) {
                int tmp = (dp[j] + vt[i]) % b;
                if (vis[tmp])   continue;
                vis[tmp] = true;
                dp.pb(tmp);
                if (tmp == c)   f = true;
            }
        }
        printf("%s\n", f ? "YES" : "NO");
    }
    return 0;
}