Timus 1104 Don’t Ask Woman about Her Age

本文介绍了一个数学挑战问题:寻找一个合适的进制k,使得给定的字符串表示的数值,在该进制下可以被k-1整除。文章详细解释了解决这个问题的方法,包括如何将字符转换为数值,以及如何通过计算来确定最小的符合条件的进制。


Description

Mrs Little likes digits most of all. Every year she tries to make the best number of the year. She tries to become more and more intelligent and every year studies a new digit. And the number she makes is written in numeric system which base equals to her age. To make her life more beautiful she writes only numbers that are divisible by her age minus one. Mrs Little wants to hold her age in secret.
You are given a number consisting of digits 0, …, 9 and Latin letters A, …, Z, where A equals 10, B equals 11 etc. Your task is to find the minimal number k satisfying the following condition: the given number, written in k-based system is divisible by k−1.

Input

Input consists of one string containing no more than 10 6 digits or uppercase Latin letters.

Output

Output the only number k, or "No solution." if for all 2 ≤ k ≤ 36 condition written above can't be satisfied. By the way, you should write your answer in decimal system.

Sample Input

         A1A          22


题意:输入一个k base的数,能被k-1整除,求k。

思路:需要注意下列知识点要记住

(1) k base的数能除掉k-1,那么这个数各位加和也能除掉k-1

(2) k一定大于各个位的数(也就是各位的最后的max)


注意:还要考虑到特殊情况各位全为0,最小就为2


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;

//将字符转化为数
int cl(char c)
{
	int r;
	if(isalpha(c)) r=c-'A'+10;   //isalpha函数判别该字符是否为字母
	else r=c-'0';
	return r;
}

int main()
{
	string a;
	int m,max_w=-1,sum=0,flag=0;
    cin>>a;
    for(int i=0;i<a.size();i++)
	{
	    m=cl(a[i]);
	    sum+=m;
	    max_w=max(max_w,m);
	}
	if(max_w==0)    //全为0的情况
	{
		cout<<"2\n"<<endl;
		return 0;
	}
	for(;max_w<36;max_w++)
	{
		if(sum%max_w==0)
		{
			flag=1;
			break;
		}
	}
	if(flag) printf("%d\n",max_w+1);
	else printf("No solution.\n");
	return 0;
}

先展示下效果 https://pan.quark.cn/s/a4b39357ea24 遗传算法 - 简书 遗传算法的理论是根据达尔文进化论而设计出来的算法: 人类是朝着好的方向(最优解)进化,进化过程中,会自动选择优良基因,淘汰劣等基因。 遗传算法(英语:genetic algorithm (GA) )是计算数学中用于解决最佳化的搜索算法,是进化算法的一种。 进化算法最初是借鉴了进化生物学中的一些现象而发展起来的,这些现象包括遗传、突变、自然选择、杂交等。 搜索算法的共同特征为: 首先组成一组候选解 依据某些适应性条件测算这些候选解的适应度 根据适应度保留某些候选解,放弃其他候选解 对保留的候选解进行某些操作,生成新的候选解 遗传算法流程 遗传算法的一般步骤 my_fitness函数 评估每条染色体所对应个体的适应度 升序排列适应度评估值,选出 前 parent_number 个 个体作为 待选 parent 种群(适应度函数的值越小越好) 从 待选 parent 种群 中随机选择 2 个个体作为父方和母方。 抽取父母双方的染色体,进行交叉,产生 2 个子代。 (交叉概率) 对子代(parent + 生成的 child)的染色体进行变异。 (变异概率) 重复3,4,5步骤,直到新种群(parentnumber + childnumber)的产生。 循环以上步骤直至找到满意的解。 名词解释 交叉概率:两个个体进行交配的概率。 例如,交配概率为0.8,则80%的“夫妻”会生育后代。 变异概率:所有的基因中发生变异的占总体的比例。 GA函数 适应度函数 适应度函数由解决的问题决定。 举一个平方和的例子。 简单的平方和问题 求函数的最小值,其中每个变量的取值区间都是 [-1, ...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值