hdu 1153 magic bitstrings

Magic Bitstrings

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 51    Accepted Submission(s): 29


Problem Description
A bitstring, whose length is one less than a prime, might be magic. 1001 is one such string. In order to see the magic in the string let us append a non-bit x to it, regard the new thingy as a cyclic string, and make this square matrix of bits 

each bit 1001 
every 2nd bit 0110 
every 3rd bit 0110 
every 4th bit 1001 

This matrix has the same number of rows as the length of the original bitstring. The m-th row of the matrix has every m-th bit of the original string starting with the m-th bit. Because the enlarged thingy has prime length, the appended x never gets used. 

If each row of the matrix is either the original bitstring or its complement, the original bitstring is magic. 

Each line of input (except last) contains a prime number p ≤ 100000. The last line contains 0 and this line should not be processed. For each prime number from the input produce one line of output containing the lexicographically smallest, non-constant magic bitstring of length p-1, if such a string exists, otherwise output Impossible. 
 

Sample Input
5 3 17 47 2 79 0
 

Sample Output
0110 01 0010111001110100 0000100001101010001101100100111010100111101111 Impossible 001001100001011010000001001111001110101010100011000011011111101001011110011011
 

Source
 

Recommend
Eddy
 


把矩陣列出來

a[1%n], a[2%n], a[3%n], ..., a[n-1]                  (1)

a[2%n], a[4%n], a[6%n], ..., a[2(n-1)%n]       (2)

a[3%n], a[6%n], a[9%n], ..., a[3(n-1)%n]       (3)

...


比較 (1), (2)

發現:

如果 a[1%n] != a[2%n],那麼 a[2%n] != a[4%n],那麼 a[1%n] == a[4%n];

如果 a[1%n] == a[2%n],那麼 a[2%n] == a[4%n],那麼 a[1%n] == a[4%n]。

所以 a[1%n] == a[4%n]

同樣的方法得到:

a[1%n] == a[9%n],

a[1%n] == a[16%n],

....

所有下標是 i 平方 mod n 都相等。

下標不是 i 平方 mod n 都相等。


爲了字典順序最小,並且避免整個數列都是同一個數(題目要求 non-constant),令 a[1%n] = a[4%n] = a[9%n] = ... = 0,其他數都是 1,這樣符合題意。


#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>

int main () {
    long long p;
    while ((std::cin >> p) && p) {
        if (p == 2) {
            std::cout << "Impossible\n";
            continue;
        }
        std::vector <int> v (p, 1);
        for (long long i=1; i<p; ++i) {
            v [i * i % p] = 0;
        }
        std::copy (v.begin() + 1, v.end(), std::ostream_iterator <int> (std::cout));
        std::cout << "\n";
    }
    return 0;
}



發佈了94 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章