Timus 1104. Don’t Ask Woman about Her Age题解

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 106 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 output
A1A
22

本题主要考两个知识点:

1 能除尽k base数中k-1的数,必然各位加起来也能除尽k-1

2 一个数的base必然大于一个位的数(不能等于)

尤其是第一个知识点,不知道的话程序就会变得很复杂或者是甚至解不出来。

还有注意特例的时候:全零

int maxKNum(string s, int &k)
{
	int sum = 0, a = 0;
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] >= ‘A‘) a = s[i] - ‘A‘ + 10;
		else a = s[i] - ‘0‘;
		k = max(k, a);
		sum += a;
	}
	return sum;
}

void DonAskWomanaboutHerAge1104()
{
	string s;
	cin>>s;

	int k = 0;
	int sum = maxKNum(s, k);
	if (0 == k) k++;//特例:全零的时候
	for ( ; k < 36; k++)
	{
		if (sum % (k) == 0)
		{
			cout<<k+1;
			return;
		}
	}
	cout<<"No solution.";
}
时间: 2024-10-08 10:01:40

Timus 1104. Don’t Ask Woman about Her Age题解的相关文章

ural 1104. Don’t Ask Woman about Her Age暴力

1104. Don’t Ask Woman about Her Age Time limit: 1.0 secondMemory limit: 64 MB 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 di

1104. Don’t Ask Woman about Her Age(数论)

a*b^n(mod(b-1) =a(mod(b-1) http://acm.timus.ru/problem.aspx?space=1&num=1104 1 #include <stdio.h> 2 #include <string.h> 3 4 char str[1000000 + 10]; 5 6 int CharToInt(char c) 7 { 8 if(c>='0' && c<='9') 9 return c - '0'; 10 ret

URAL 1104 Don’t Ask Woman about Her Age(数论)

题目链接 题意 : 给你一个数,未知进制,然后让你从2到36进制中找出一个最小的进制K,满足给你的这个数作为k进制时能够整除k-1. 思路 : 有一个公式,(a*b^n)mod(b-1)=a: 给定你这个数,当做字符串输入的时候,这个数转化成10进制的结果应该是:a[0]*k^(n-1)+a[1]*k^(n-2)+……+a[n-1]*k^0,然后题目要求的就是这个式子的结果取余(k-1)为0, 经过最开始给出的公式,将该式子化简得(a[0]+a[1]+……+a[n-1])%(k-1),所以只要满

Cocos2d-x的生成Json文件的方法(续)

本文承接自前文:http://blog.csdn.net/yuxikuo_1/article/details/39155335 1.JsonMake类 //.h #include "cocos2d.h" #include "../cocos2d/external/json/document.h" #include "../cocos2d/external/json/writer.h" #include "../cocos2d/exter

WebApi传参总动员(二)

上篇,从最简单的string入手.本篇演示了从请求的输入流中获取实体.api: public class ValuesController : ApiController { [HttpPost] public string GetData(string name) { return "我爱" + name; } [HttpPost] public string GetData() { var stream = HttpContext.Current.Request.InputStre

《SQL语句测试》

新建一张学员信息表(student),要求:1.字段如下:学号(sid),姓名(name),性别(sex),年龄(age),地址(address).2.分别为字段添加约束:学号为主键,姓名为非空,性别为检查约束,年龄为检查约束,地址为默认约束. SQL>create table student( sid int constraint student_sid_pk primary key, name varchar2(20) constraint student_name_nn not null,

Python之继承

继承是所有开发语言的必修内容,而本文写的只是Python继承中的特殊之处,关于继承概念及内容可以自行百度(不装B,感觉百度挺好的) 1.构造函数: 要说继承,先要说一下构造函数.Java要求是与类名相同并且无返回值,而Python则是强制要求命名为"__init__()". 当创建类的对象时,会自动先调用构造函数,一般用于初始化.构造函数可以不写,那么程序会隐式自动增加一个空的构造函数. 2.继承写法: (1).class 空格 类名称 括号内填写父类名 冒号 具体写法如下 class

vue-ajax小封装

1. js 文件: /** ajax封装:* 1. 引入文件* 2. new Vue().ajax.get(url,data,fn,ojson), 或 new Vue().ajax.post(url,data,fn,ojson)* url: 需要获取数据的文件地址 (string)* data: 需要发送的信息 (可省略) (obj)* fn: 获取信息后的回调函数,接收到的返回值为data (function)* ojson: 是否需要转换为json格式 (可省略) (设置为 "json&qu

WebApi传参总动员(四)

前文介绍了Form Data 形式传参,本文介绍json传参. WebApi及Model: public class ValuesController : ApiController { [HttpPost] public string GetData(string name,[FromBody]Woman woman) { return "我是" + name + ",我喜欢" + woman.Name; } [HttpPost] public string Ge