poj 1142 Smith Numbers(数论:欧拉函数变形)

给定一个数n找出大于n的最小smith数

smith数定义如下:

一个数n为smith数当且仅当它的所有质因子各位数之和等于n的所有位数之和且n不是素数

那么给定一个n,我们就可以每次+1判断是否为smith数

这道题唯一的难点就在于找到一个数的所有素数因子

套用欧拉函数变形即可

375ms代码如下:

#include <math.h>
#include <stdio.h>
#define LL long long

LL n;

int get_ans(LL n) {
    int ans = 0;
    while(n) {
        ans += n%10ll;
        n /= 10ll;
    }
    return ans;
}

int judge(LL n) {
    LL i;
    int ans1, ans2, tmp;
    tmp = n;
    ans2 = 0;
    ans1 = get_ans(n);
    int m = sqrt(n+0.5);
    for(i=2; i<=m; ++i) {//欧拉函数n变形
        while(n % i == 0) {
            ans2 += get_ans(i);
            n /= i;
        }
    }
    if(n == tmp) return 0;//n不为素数
    if(n > 1) ans2 += get_ans(n);
    //printf("ans1 = %d\tans2 = %d\n", ans1, ans2);
    return ans1 == ans2 ? 1 : 0;
}

int main(void) {
    while(scanf("%lld", &n) && n) {
        while(!judge(++n));
        printf("%lld\n", n);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值