Codeforce 460b little Dima and Equation

Source: Internet
Author: User

This question provides a sharp formula X = B · S (X)A+ C, S (x) is the sum of the individual digits of X, and the formula value is found between 0-000000000. Obviously, the brute force enumeration definitely times out, and it takes one minute to traverse one side, therefore, we need to analyze the formula.

We can see that S (x) ^ A = (X-C)/B, that is, (x-C)/B must be an integer, so the loop can be written as for (INT I = C; I <1000000000; I + = B), but this optimization is far from enough.

The above logic is to traverse the value of X. Take a closer look at the formula. There is a s (x) ^ A in it, which indicates that the problem is that S (x) can be traversed ), in this way, the traversal range is reduced to 0-85. Why? The sum of the bits of the maximum value of 999999999 is 81, so for (INT I = 1; I <85; I ++) traverse S (x), then use X = S (x) ^ A * B + C to X, and then check whether the sum of all bits of X is = I. Here s (X) ^ a * B + C note that this value will exceed int, so long is used. During the game, WA is here, and the POW function is hard to use in codeforce, I handed it over to WA, and it also made me wa twice, jiong

#include<iostream>#include<stdio.h>#include<math.h>using namespace std;int tsum(int i){    int sum=0;    while(i){        sum+=(i%10);        i/=10;    }    return sum;}long long powd(int t,int n){    long long sum=1;    for(int i=1;i<=n;i++)        sum*=t;    return sum;}int main(){    int a,b,c;    while(~scanf("%d%d%d",&a,&b,&c)){        int cou=0;        int aa[100000];        for(int i=1;i<85;i++){            long long t=powd(i,a);            long long x=t*b+c;            if(x>0 && x<1000000000 && tsum(x)==i){                aa[cou++]=x;            }        }        printf("%d\n",cou);        for(int i=0;i<cou;i++){            if(i==(cou-1))                printf("%d\n",aa[i]);            else                printf("%d ",aa[i]);        }    }}

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: 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.