Smith Numbers
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13802 | Accepted: 4695 |
Description
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 the sum of the digits of the prime factors of that number. Got it? Smith's telephone number was 493-7775. This number can be written as the product of its prime factors in the following way:
4937775= 3*5*5*65837
The sum of all digits of the telephone number is 4+9+3+7+7+7+5= 42,and the sum of the digits of its prime factors is equally 3+5+5+6+5+8+3+7=42. Wilansky was so amazed by his discovery that he named this kind of numbers after his brother-in-law: Smith numbers.
As this observation is also true for every prime number, Wilansky decided later that a (simple and unsophisticated) prime number is not worth being a Smith number, so he excluded them from the definition.
Wilansky published an article about Smith numbers in the Two Year College Mathematics Journal and was able to present a whole collection of different Smith numbers: For example, 9985 is a Smith number and so is 6036. However,Wilansky was not able to find a Smith number that was larger than the telephone number of his brother-in-law. It is your task to find Smith numbers that are larger than 4937775!
The sum of all digits of the telephone number is 4+9+3+7+7+7+5= 42,and the sum of the digits of its prime factors is equally 3+5+5+6+5+8+3+7=42. Wilansky was so amazed by his discovery that he named this kind of numbers after his brother-in-law: Smith numbers.
As this observation is also true for every prime number, Wilansky decided later that a (simple and unsophisticated) prime number is not worth being a Smith number, so he excluded them from the definition.
Wilansky published an article about Smith numbers in the Two Year College Mathematics Journal and was able to present a whole collection of different Smith numbers: For example, 9985 is a Smith number and so is 6036. However,Wilansky was not able to find a Smith number that was larger than the telephone number of his brother-in-law. It is your task to find Smith numbers that are larger than 4937775!
Input
The input file consists of a sequence of positive integers, one integer per line. Each integer will have at most 8 digits. The input is terminated by a line containing the number 0.
Output
For every number n > 0 in the input, you are to compute the smallest Smith number which is larger than n,and print it on a line by itself. You can assume that such a number exists.
Sample Input
4937774
0
Sample Output
4937775
Source
- 要求是非素数,用Miller-Rabin测试来进行判定
- 分解质因数
- 分解的时候注意一下,质数中目前只有2一个偶数,所以自3开始都要加2,这样省去了一半的时间
for(LL i=2;i<=t;i+=(2==i?1:2))
- 然后检测下是否相等就行
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1000 + 10 ;
const int inf = 0x3f3f3f3f ;
const int npos = -1 ;
const int mod = 1e9 + 7 ;
const int mxx = 100 + 5 ;
const double eps = 1e-6 ;
LL pow_mod(LL x, LL k, LL m){
LL r=1%m;
while(k){
if(1&k)r=((r*x)%m + m)%m;
x=((x*x)%m + m)%m;
k>>=1;
}
return r;
}
int test_prime(LL n, LL a, LL d){
if(2==n)return 1;
if(a==n)return 1;
if(0==(1&n))return 0;
while(!(1&d))d>>=1;
LL t=pow_mod(a,d,n);
while((d!=n-1) && (1!=t) && (t!=n-1)){
t=(LL)(((t*t)%n + n)%n);
d<<=1;
}
return t==n-1 || 1==(1&d);
}
int isprime(LL n){
if(2>n)return 0;
LL a[]={2,3,61};
for(int i=0;i<3;i++)
if(!test_prime(n,a[i],n-1))
return 0;
return 1;
}
void factors(LL n, vector<LL> &a, vector<LL> &b, int &tot){
tot=0;
a.clear();
b.clear();
LL t=(LL)((double)sqrt(n)+1);
for(LL i=2;i<=t;i+=(2==i?1:2))
if(0==n%i){
a.push_back(i);
b.push_back(0);
while(0==n%i){
b[tot]++;
n/=i;
}
tot++;
}
if(1!=n){
a.push_back(n);
b.push_back(1);
tot++;
}
}
LL sumdigits(LL x){
LL r=0;
while(x){
r+=(x%10);
x/=10;
}
return r;
}
int isSmith(LL x){
LL l=sumdigits(x), r=0;
std::vector<LL> a, b;
int tot;
factors(x,a,b,tot);
for(int i=0;i<tot;i++){
r+=b[i]*sumdigits(a[i]);
}
return l==r;
}
LL n;
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%lld",&n) && n){
while(++n)
if(!isprime(n) && isSmith(n)){
printf("%lld\n",n);
break;
}
}
return 0;
}