UVA - 1203 Argus 直接爆

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
// cout << "  ===  " << endl;

using namespace std;
typedef long long ll;
const int maxn = 1000 + 7, INF = 0x3f3f3f3f, mod = 1e9+7;
int n, k;

struct node {
    int m;
    int p;
}a[maxn];

int getgcd(int a, int b) {
    return b == 0 ? a : getgcd(b, a%b);
}

bool cmp(node a, node b) {
    return a.m < b.m;
}

void solve() {
    sort(a, a+n, cmp);

    int gcd = a[0].p;
    for(int i = 1; i < n; ++i) {
        gcd = getgcd(gcd, a[i].p);
        if(gcd == 1) break;
    }
    int t = gcd, cnt = 0;
    while(1) {
        for(int i = 0; i < n; ++i) {
            if(t % a[i].p == 0) {cout << a[i].m << endl; cnt++; }
            if(cnt == k) break;
        }
        if(cnt == k) break;
        t += gcd;
    }
}

int main() {

    ios::sync_with_stdio(false);
    n = 0;
    string s;
    while(cin >> s) {
        if(s[0] == '#') break;
        int m, p;
        cin >> a[n].m >> a[n].p;
        n++;
    }
    cin >> k;
    solve();

    return 0;
}

你可能感兴趣的:(UVA - 1203 Argus 直接爆)