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

1104. Don’t Ask Woman about Her Age

Time limit: 1.0 second
Memory 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 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

Problem Author: Igor Goldberg
Problem Source: Tetrahedron Team Contest May 2001

思路:从小到大暴力,注意年龄 >= 2

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <string>
using namespace std;
int N, T;
int main() {
    //freopen("in.txt", "r", stdin);
    string s;
    cin >> s;
    int cnt = 2;
    for(int i = 0; i < s.size(); i++){
        if(s[i] >= ‘A‘ && s[i] <= ‘Z‘ && s[i] - ‘A‘ + 10 >= cnt){
            cnt = s[i] - ‘A‘ + 11;
        }else if(s[i] >= ‘0‘ && s[i] <= ‘9‘&& s[i] - ‘0‘ >= cnt){
            cnt = s[i] - ‘0‘ + 1;
        }
    }
    long long int sum = 0, now;
    for(int k = cnt; k <= 36; k++){
            if(s[s.size()-1] >= ‘A‘ && s[s.size()-1] <= ‘Z‘) sum = s[s.size()-1] - ‘A‘ + 10;
            else sum = s[s.size()-1] - ‘0‘;
            sum %= (k-1);
        for(int i = s.size()-2; i >= 0; i--){
             if(s[i] >= ‘A‘ && s[i] <= ‘Z‘) now = s[i] - ‘A‘ + 10;
            else now = s[i] - ‘0‘;
            sum += now*k;
            sum %= (k-1);
        }
        if(sum == 0) {
            printf("%d\n", k);
            exit(0);
        }
    }
    printf("No solution.\n");
    return 0;
}
时间: 2024-10-27 21:54:00

ural 1104. Don’t Ask Woman about Her Age暴力的相关文章

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),所以只要满

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

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 t

ural 1104,暴力取模

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1104 题目大意:输入一个字符串(数字与大写字母组成),输出n,n满足此字符串为n进制时,其n进位制数能被n-1整除(n不存在时输出"No solution"(不包括双引号)). 题目好多坑点,我也是WA了好几次才A的.算法是暴力的. 1,刚开始,我怎么都想不清楚,答案就是2啊,任何数都可以被1整除啊,其实,不是这样的,比如说,123就不是2进制数,所以说,这里要找到字符串中最

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