B. Little Dima and Equation

本文探讨了一道数学难题,即在给定的等式 x = b * s(x)^a + c 中找到所有符合条件的整数解 x(0 < x < 10^9),其中 a, b, c 是已知常数,s(x) 表示 x 的十进制表示中各位数字之和。通过分析和枚举,我们找到了解题的关键步骤和具体实现方法。
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment.

Find all integer solutions x (0 < x < 109) of the equation:

x = b·s(x)a + c, 

where abc are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x.

The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: abc. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem.

Input

The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000).

Output

Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 109.

Sample test(s)
input
3 2 8
output
3
10 2008 13726
input
1 2 -18
output
0
input
2 2 -1
output
4
1 31 337 967

题目地址:http://codeforces.com/contest/460/problem/B

这题乍一看没思路,但是仔细分析下会发现,s(x)是一个从1到81的数,无论x是多少。所以可以枚举1到81,这样就转化成了一个一元一次方程,直接求解x就可以了。这时候还要判断x是否在1到10^9之间,并且它的各位数之和是s(x)。

这题脑残了两次。。。第一次是写成了<=10^9。。然后发现错误后,就又改了回来。。但是发现10^9是不可能的

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <algorithm>
#include <queue>
using namespace std;
#define LL __int64
LL aa[100];
int main()
{
    LL n, a, b, c, ans=0, i, y, zz, j;
    LL x, z;
    scanf("%I64d%I64d%I64d",&a,&b,&c);
    for(i=1;i<=81;i++)
    {
        zz=1;
        for(j=1;j<=a;j++)
            zz*=i;
        x=zz*b+c;
        y=0;
        z=x;
        while(z)
        {
            y+=z%10;
            z/=10;
        }
        if(y==i&&x>=1&&x<1e9)
        {
            aa[ans++]=x;
        }
    }
    printf("%I64d\n",ans);
    for(i=0;i<ans;i++)
    {
        printf("%I64d ",aa[i]);
    }
    return 0;
}


转载于:https://www.cnblogs.com/767355675hutaishi/p/3926447.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值