poj1152

简单题

View Code
#include <iostream>
#include
<cstdlib>
#include
<cstring>
#include
<cstdio>
using namespace std;

int value[300];
char st[1000000];

bool ok(int n)
{
int len = strlen(st);
for (int i = 0; i < len; i++)
if (value[st[i]] >= n)
return false;
int temp = 0;
int w = n - 1;
for (int i = 0; i < len; i++)
temp
= (temp * n % w + value[st[i]] % w) % w;
return temp == 0;
}

int main()
{
//freopen("t.txt", "r", stdin);
for (char ch = '0'; ch <= '9'; ch++)
value[ch]
= ch - '0';
for (char ch = 'A'; ch <= 'Z'; ch++)
value[ch]
= ch - 'A' + 10;
for (char ch = 'a'; ch <= 'z'; ch++)
value[ch]
= ch - 'a' + 36;
while (scanf("%s", st) != EOF)
{
int ans = -1;
for (int i = 2; i <= 62; i++)
if (ok(i))
{
ans
= i;
break;
}
if (ans == -1)
printf(
"such number is impossible!\n");
else
printf(
"%d\n", ans);
}
return 0;
}
posted @ 2011-08-29 18:58  金海峰  阅读(270)  评论(0编辑  收藏  举报