zoukankan      html  css  js  c++  java
  • ACM: Gym 101047B Renzo and the palindromic decoration

     Gym 101047B  Renzo and the palindromic decoration
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    standard input/output

    At the ruins of Wat Phra Si Sanphet (วดพระศรสรรเพชญ), one can find famous inscriptions that have only recently been decoded. Several numbers written using Thai numerals decorate these ruins.

    A couple of years ago, the famous Peruvian researcher Renzo "el intrépido" Morales found out that most numbers found at the ruins are palindromic, that is, they represent the same number when read backwards. For instance, 171 is palindromic, whereas 17 is not.

    Intrigued by the existence of non palidromic numbers as decorations in the ruins, Renzo found out that, while these numbers are not palindromic when represented in base 10 (which is the base used in the Thai numeral system), they are palindromic when represented in another base. For b > 0, the base-b representation of a number N is the sequence amam - 1... a1a0 so that 0 ≤ ai ≤ b - 1 for each 0 ≤ i ≤ mam ≠ 0, and

    ambm + am - 1bm - 1 + ... + a1b + a0 = N.
    For the previous example, the base-2 representation of 17 is 10001, which is palindromic.

    To validate his discovery, Renzo wants you to write a program that takes a number represented in base 10 and checks in which bases from 2 to 16 such number has a palindromic representation.

    Input

    The first line has a single integer T, the number of test cases.

    Each test case has a single line with an integer N written in base 10.

    Output

    For each test case, print in a single line a space-separated list of integers, from 2 to 16 and in increasing order, of the bases for which the representation of N is palindromic. If N does not have a palindromic representation for any of the bases from 2 to 16, print -1.

    Limits

    • 0 ≤ T ≤ 103
    • 0 ≤ N < 231

    Sample Input

    Input
    2
    17
    2570
    Output
    2 4 16
    4 16

    /*/
    题意:
    给你一个数,将这个数换成2~16进制的任何数,如果换成其他进制的数之后能够形成一个回文数【不懂去百度】就输出这个进制。

    如果一个都不能形成回文数,输出 -1 。 模拟短除法,暴力解题。 AC代码: /
    */

    #include"algorithm"
    #include"iostream"
    #include"cstring"
    #include"cstdio"
    #include"string"
    #include"vector"
    #include"queue"
    #include"cmath"
    using namespace std;
    #define FK(x) cout<<"["<<x<<"]"<<endl
    #define memset(x,y) memset(x,y,sizeof(x))
    #define memcpy(x,y) memcpy(x,y,sizeof(x))
    #define bigfor(x) for(int qq=1;qq<=x;qq++)
    #define FIN freopen("input.txt","r",stdin)
    #define FOUT freopen("output.txt","w+",stdout)
    
    int ans[20];
    
    bool ok(int n,int i) {
    	memset(ans,0);
    	int erear=0;
    	while(n) {
    		ans[erear++]=n%i;
    		n/=i;
    	}
    	for(int j=0; j<erear/2; j++) {
    		if(ans[j]!=ans[erear-1-j])return 0;
    	}
    	return 1;
    }
    
    int main() {
    	int T;
    	scanf("%d",&T);
    	bigfor(T) {
    		int n;
    		scanf("%d",&n);
    		int first=1;
    		int sign=1;
    		for(int i=2; i<17; i++) {
    			if(ok(n,i)) {
    				if(first) first=0;
    				else printf(" ");
    				printf("%d",i);
    				sign=0;
    			}
    		}
    		if(sign)puts("-1");
    		else puts("");
    	}
    	return 0;
    }
    
    
    

      

     
  • 相关阅读:
    14_java之变量|参数|返回值|修饰符
    NYOJ 202 红黑树 (二叉树)
    NYOJ 138 找球号(二) (哈希)
    NYOJ 136 等式 (哈希)
    NYOJ 133 子序列 (离散化)
    NYOJ 129 树的判定 (并查集)
    NYOJ 117 求逆序数 (树状数组)
    NYOJ 93 汉诺塔 (数学)
    HDU 2050 折线分割平面 (数学)
    天梯赛L2-008 最长对称子串 (字符串处理)
  • 原文地址:https://www.cnblogs.com/HDMaxfun/p/5796873.html
Copyright © 2011-2022 走看看