您的位置:首页 > 编程语言 > PHP开发

PKU 1152 An Easy Problem!

2009-08-27 20:47 330 查看
Description
Have you
heard the fact "The base of every normal number system is 10" ? Of
course, I am not talking about number systems like Stern Brockot Number
System. This problem has nothing to do with this fact but may have some
similarity.

You will be given an N based integer number R and you are given the
guaranty that R is divisible by (N-1). You will have to print the
smallest possible value for N. The range for N is 2 <= N <= 62
and the digit symbols for 62 based number is (0..9 and A..Z and a..z).
Similarly, the digit symbols for 61 based number system is (0..9 and
A..Z and a..y) and so on.
Input
Each
line in the input will contain an integer (as defined in mathematics)
number of any integer base (2..62). You will have to determine what is
the smallest possible base of that number for the given conditions. No
invalid number will be given as input. The largest size of the input
file will be 32KB.
Output
If
number with such condition is not possible output the line "such number
is impossible!" For each line of input there will be only a single line
of output. The output will always be in decimal number system.
Sample Input
3

5

A

Sample Output
4

6

11

 题目其实很简单,但是很有趣。假设一个数可以表示为∑ai * (base ^ i),这个数可以被n-1整除,那么∑ai也能被n-1整除,而且两者是等价的。

 

//#include "stdafx.h"
//============================================================================
// Name        : acmcpp.cpp
// Author      : Yubo Chow
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <math.h>
#include <algorithm>
#include <set>
#include <limits.h>
#include <float.h>
#include <bitset>
#include <cmath>
#include <vector>
#include <map>
#include <list>
#include <string.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("case.txt", "r", stdin);
#endif
char num[320000];
while (scanf("%s", num) == 1 && num[0])
{
int len = 0, base = 1;
int sum = 0;
for (; num[len]; ++len)
{
if (num[len] <= '9')
{
num[len] -= '0';
}
else if (num[len] <= 'Z')
{
num[len] -= 'A' - 10;
}
else
{
num[len] -= 'a' - 36;
}
base = max(base, int(num[len]));
sum += num[len];
}
for (++base; base < 63; ++base)
{
if (sum % (base - 1) == 0)
{
cout << base << endl;
break;
}
}
if (base == 63)
{
cout << "such number is impossible!" << endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息