POJ 2826 An Easy Problem!(简单数论)

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

题意是:对于给定数字。能否使他是N进制,而且满足被N-1整除。假设能找到这样一个数N。那么输出N,否则输出such number is impossible!
首先,举个例 假如这个数是N进制的 2 A C D,这个数值为(2*N*N*N+A*N*N*+C*N+D),再对N-1取模:
先对第一项2*N*N*N取模,2%(N-1)N%(N-1)*N%(N-1)*N%(N-1)。N%(N-1)=1,所以第一项的值为2%(N-1),依据这个结果,能够得出(2*N*N*N+A*N*N+C*N+C)%(N-1)=(2+A+C+D)%(N-1)
那么这道题仅仅需把每一位数字的值加起来再对(N-1)取模就可以。
枚举N的值,推断是否取模结果为0。还要对N的范围加以限定。

#include<iostream>
#include<stdio.h>
#include<queue>
#include<stack>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
using namespace std;
int num[30005];
char s[30005];
int main()
{
    while(scanf("%s",&s)!=EOF)
    {
        int len=strlen(s);
        int Max=0;
        for(int i=0;i<len;i++)
        {
            if(s[i]>='0'&&s[i]<='9')
                num[i]=s[i]-'0';
            else if(s[i]>='A'&&s[i]<='Z')
                num[i]=s[i]-'A'+10;
            else if(s[i]>='a'&&s[i]<='z')
                num[i]=s[i]-'a'+36;
            Max=max(Max,num[i]);//比方输入为A,那么进制至少从A開始寻找满足要求的进制
        }
        if(Max==0)
        {
            printf("2\n");//假设输入的数最大为0,那么仅仅能是2进制
        }
        int sum=0;
        for(int i=0;i<len;i++)
        {
            sum+=num[i];
        }
        int flag=100;
        for(int i=Max;i<=62;i++)
        {
            if(sum%i==0)
            {
                flag=i;
                break;
            }
        }
        if(flag<=61)
        {
            printf("%d\n",flag+1);
        }
        else
        {
            printf("such number is impossible!\n");//由于仅仅有62进制。大于61就不满足题目条件了
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/mfmdaoyou/p/7053003.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值