hdu1153 Magic Bitstrings

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
题意:给一个素数n,求这样的字符串,长度为n-1,在尾部添加一个字符x,然后执行n-1次这样的操作:每k(1到n-1)个字符中提取一个字符,构成长度为n-1的新字符串。这样,就形成了n-1行的长度为n-1的字符串。其中的字符串要么是原来的字符串,要么是补字符串,也就是说,结构中只有两种字符串,字符不能全部相同。(字符只包含0和1)
思路:
每1个字符中提取后,新的字符串为:
a[1%n], a[2%n], a[3%n],…,a[(n-1)%n];
第2个字符中提取后,新的字符串为:
a[2%n], a[4%n], a[6%n],…,a[2(n-1)%n];
如果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[9%n]…==a[i *i % n]
代码如下:

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
       Scanner cin = new Scanner(System.in);

       while (true) {
           int n = cin.nextInt();
           if (n == 0) break;
           if (n == 2) {
               System.out.println("Impossible");
           } else {
               StringBuilder sb = new StringBuilder();
               for (int i = 0; i < n; i++) sb.append('1');


               for (int i = 1; i < n; i++){
                   sb.setCharAt((int)((long)i * (long)i % n), '0');
               }

               String s = sb.substring(1);
               System.out.println(s);
           }
       }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值