GCJ Qualification Round 2016 C題

題意是給定了一個叫「jamcoin」的定義,讓你生成足夠數量知足條件的jamcoin。ios

jamcoin其實就能夠理解成一個二進制整數,題目要求的要麼長度爲16位,要麼爲32位,一頭一尾兩個位必須是1,而後就是這個數字串在各類進制下表示的數都不能是質數。spa

個人作法很簡單,由於大體口算了一下,知足條件的jamcoin應該挺多的,隨便找50個或500個就行了。就從最小的串開始檢查,找到一個就輸出一個,直到找到要求的個數爲止。code

檢查的方法也很簡單,就是把這個串表示的數用素數表去除,能找到整數這個數的,就記下來,不然就認爲這個串不知足條件。爲了加快速度,個人素數表很小,只有1000之內的素數。blog

固然,由於當N爲32時,串表示的數會超過long long,因此不必先表數表示成整數再去除,而是直接用快速冪取餘去作便可。好比100011,做爲二進制串,(2^5+2^1+2^0) % 5 = 0string

/*
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
#include <bitset>
using namespace std;
typedef long long LL;
int modular_exp(int a, int b, int c) {
    LL res, temp;
    res = 1 % c, temp = a % c;
    while (b) {
        if (b & 1) {
            res = res * temp % c;
        }
        temp = temp * temp % c;
        b >>= 1;
    }
    return (int) res;
}

int N, J;
int arr[11];
const int ptn = 168;
const int pt[ptn] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997};

bool isprime(LL d, int k) {
    for (int i = 0; i < ptn; i++) {
        int p = pt[i];
        int mod = 0;
        LL tmp = d;
        int b = 0;
        while (tmp > 0) {
            if (tmp % 2 == 1) {
                mod += modular_exp(k, b, p);
                mod %= p;
            }
            b++;
            tmp /= 2;
        }
        if (mod == 0) {
            arr[k] = p;
            return false;
        }
    }
    return true;
}

bool judge(LL d) {
    for (int k = 2; k <= 10; k++) {
        if (isprime(d, k)) {
            return false;
        }
    }
    return true;
}

void work() {
    printf("Case #1:\n");
    LL s = (1LL << (N - 1)) + 1;
    LL e = 1LL << N;
    int pn = 0;
    for (; s < e && pn < J; s += 2) {
        if (judge(s)) {
            if (N == 16) {
                bitset<16> bs(s);
                cout << bs;
            } else {
                bitset<32> bs(s);
                cout << bs;
            }
            for (int i = 2; i <= 10; i++) {
                printf(" %d", arr[i]);
            }
            putchar('\n');
            pn++;
        }
    }
}

int main1() {
    if (judge(35)) {
        cout << "true!" << endl;
    } else {
        cout << "false" << endl;
    }
    return 0;
}

int main() {
    int T;
    scanf("%d", &T);
    for (int t = 1; t <= T; t++) {
        scanf("%d %d", &N, &J);
        work();
    }
    return 0;
}
相關文章
相關標籤/搜索