HackerRank - "Identify Smith Numbers"

I guess the punch line of this one is Sieving for primes.

#include <cmath>
#include <cstdio>
#include <climits>
#include <cctype>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

vector<int> primes;

int digiSum(int n)
{
    int ret = 0;
    while (n)
    {
        ret += n % 10;
        n /= 10;
    }
    return ret;
}

int facSum(int n)
{
    int ret = 0;

    int pinx = 0;
    while (n > 1 && pinx < primes.size())
    {
        int p = primes[pinx];
        while (n % p == 0)
        {
            n /= p;
            ret += digiSum(p);
        }
        pinx++;
    }
    if (n > 1)
    {
        ret += digiSum(n);
    }
    return ret;
}

void go(int n)
{
    int dSum = digiSum(n);
    int fSum = facSum(n);
    cout << (dSum == fSum ? 1 : 0) << endl;
}

void sieving(int n)
{
    int bound = ceil(sqrt(n));
    vector<bool> mark(bound + 1, true);

    int removed = 1;
    int picked = 2;
    while (removed)
    {
        removed = 0;
        for (int i = 2 * picked; i <= bound; i += picked)
        {
            mark[i] = false;
            removed++;
        }

        picked++;
        while (!mark[picked]) picked++;
    }    

    for (int i = 2; i < mark.size(); i ++)
        if (mark[i])
            primes.push_back(i);
}

int main()
{
    sieving(INT_MAX);
    int n; cin >> n;
    go(n);
    return 0;
}
时间: 2024-10-12 20:46:24

HackerRank - "Identify Smith Numbers"的相关文章

Identify Smith Numbers

Link: https://www.hackerrank.com/challenges/identify-smith-numbers 1 def sum_digits(n): 2 return sum(int(x) for x in str(n)) 3 4 def prime_factors(n): 5 factors = [] 6 for i in xrange(2, n): 7 if i*i > n: 8 break 9 elif n % i == 0: # 短除法核心 10 while n

UVA 10042 Smith Numbers(数论)

Smith Numbers Background While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University , noticed that the telephone number of his brother-in-law H. Smith had the following peculiar property: The sum of the digits o

【HackerRank】Closest Numbers

Sorting is often useful as the first step in many different tasks. The most common task is to make finding things easier, but there are other uses also. Challenge Given a list of unsorted numbers, can you find the numbers that have the smallest absol

【HackerRank】Missing Numbers

Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very proud of these lists. Unfortunately, while transporting them from one exhibition to another, some numbers from List A got left out. Can you find out the

Smith Numbers(分解质因数)

Smith Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14173   Accepted: 4838 Description While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,noticed that the telephone number of his b

A - Smith Numbers POJ

While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,noticed that the telephone number of his brother-in-law H. Smith had the following peculiar property: The sum of the digits of that number was equal to

POJ 1142 Smith Numbers(分治法+质因数分解)

http://poj.org/problem?id=1142 题意: 给出一个数n,求大于n的最小数,它满足各位数相加等于该数分解质因数的各位相加. 思路:直接暴力. 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 #include <cmath> 7 using nam

POJ 2492 A Bug&#39;s Life

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 35756   Accepted: 11730 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different gender

poj 2492

A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, indi