UV problem 10042 Smith numbers (Smith number)

Source: Internet
Author: User
Tags dot net
// Smith numbers (Smith number) // PC/Ultraviolet IDs: 110706/10042, popularity: B, success rate: average level: 1 // verdict: accepted // submission date: 2011-06-10 // UV Run Time: 0.060 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net /// perform prime factor decomposition on the logarithm. Because the number is less than 10e9, if a certain number is a combination, then the prime factor must be at least one that is less than or equal to // SQRT (10e9), you can calculate 2-SQRT (10e9) first) the prime numbers between them are used as backups. # Include <iostream> # include <vector> # include <cmath> # include <ctime> using namespace STD; # define upbound 31623 // Ceil (SQRT (10e9 )) vector <int> prime; // stores prime numbers between 2-Ceil (SQRT (10e9) to facilitate problem solving. // Determine whether number N is a prime number. Bool is_prime (int n) {If (n = 2) return true; If (N % 2 = 0) return false; For (INT c = 3; c <= Ceil (SQRT (n); C + = 2) if (N % C = 0) return false; return true ;} // pregenerates a prime number between 2-Ceil (SQRT (10e9. Void find_prime () {Prime. push_back (2); For (INT c = 3; C <upbound; C ++ = 2) if (is_prime (c) Prime. push_back (c);} // calculates the sum of digits of number. Int digits_sum (INT number) {int sum = 0; while (number) {sum + = (Number % 10); Number/= 10;} return sum ;} void Smith (INT number) {int current = Number + 1; while (1) {// if it is a prime number, it is not a Smith number. If (is_prime (current) {current ++; Continue ;}// calculates the sum of digits of current. Int TMP = current; int sum = digits_sum (current), tsum = 0; // calculate the sum of the prime factors of the current number. While (1) {int memo = TMP; For (int c = 0; C <Prime. size (); C ++) if (TMP % prime [c] = 0) {tmp/= prime [c]; tsum + = digits_sum (prime [c]); break;} // If TMP is 1, all prime factors are found. If TMP is not changed, all prime factors are found at // 2-SQRT (10e9) between there is no TMP prime factor, TMP must be // is a prime number. If (TMP = 1 | memo = TMP) break;} // If TMP is not 1, it must be a prime number, because if it is a combination, no prime factor exists between 2-SQRT (10e9). This is impossible. In this case, all prime factors of this number are greater than SQRT (10e9 ), then // The number must be greater than 10e9, which is inconsistent with the question conditions. If (TMP> 1) tsum + = digits_sum (TMP); // you can specify whether the operators and digits are equal. If (tsum = sum) {cout <Current <Endl; break;} current ++ ;}} int main (int ac, char * AV []) {INT cases, number; find_prime (); CIN> cases; while (cases --) {CIN> Number; Smith (number) ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.