ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1104. Don’t Ask Woman about Her Age

wa4 on c++
Posted by IlushaMax 9 Jul 2016 22:00
#include <iostream>
#include <cmath>
using namespace std;
void main(){
    int a[100001];
    char s[100001];
    gets_s(s);
    int max = 0;
    for (int i = 0; i <= strlen(s); i++)
    {
        if (s[i] >= 'A'&& s[i] <= 'Z'){
            a[i] = s[i] - 55;
            if (max < a[i]){
                max = a[i];
            }
        }
        else{////if digits
            a[i] = s[i] - 48;
            if (max < a[i]){
                max = a[i];
            }
        };
    };
    int k;
    long long res;
    max++;
    bool found = 1;
    for (k = max; k <= 36; k++)
    {
        res = 0;
        for (int i = strlen(s) - 1; i >= 0; i--)
        {
            res = res + pow(k, i)*a[i];
        }
        if (res % (k - 1) == 0){
            found = 0;
            break;
        }
    }
    if (found==0){
        cout << k;
    }
    else{
        cout << "No solution.";
    }
    system("pause");
}
I think that problem in large numbers in tests, am I right?
Edited by author 09.07.2016 22:05

Edited by author 09.07.2016 22:26
Re: wa4 on c++
Posted by Filip Franik 12 Jul 2016 14:19
According to the description the input can have 10^6 digits this means that the input string can be a million characters long. This will definitely overflow even "long long" :)

There is a solution to this problem described below. They shown a proof that all the calculations that You need to do is summing up the digits. I'm experimenting with a different approach. I try to simulate "pen and paper" division, and get the remainder.

And finally I got AC with my approach.

Edited by author 12.07.2016 14:48