ในปัญหานี้ เราได้รับค่าจำนวนเต็มสามค่า A, B, C ภารกิจของเราคือ หาจำนวนคำตอบของสมการที่กำหนด
สมการ
X = B*Sm(X)^A + C
โดยที่ Sm(X) คือผลรวมของหลักของ X
เราต้องนับค่า X ทั้งหมดที่ทำให้สมการด้านบนเป็นจริง โดย X สามารถเป็นตัวเลขใดก็ได้ระหว่าง 1 ถึง 109
มาดูตัวอย่างเพื่อเข้าใจปัญหา
อินพุต
A = 3, B = 6, C = 4
เอาต์พุต
3
วิธีการแก้ปัญหา
วิธีแก้ปัญหาคือการนับจำนวนค่า X สำหรับนี้ ผลรวมของหลักเล่นบทบาทสำคัญ ผลรวมของหลักสูงสุดคือ 81 (สำหรับค่าสูงสุด 999999999) และสำหรับแต่ละค่าของผลรวม เราสามารถได้คำตอบของสมการได้
เราจะนับค่าที่ทำให้สมการเป็นจริงจากค่า 1 ถึง 81
ตัวอย่าง
โปรแกรมสาธิตการทำงานของวิธีแก้ปัญหาของเรา
#include <bits/stdc++.h> using namespace std; int countSolutions(int a, int b, int c){ int solutionCount = 0; for (int digSum = 1; digSum <= 81; digSum++) { int solVal = b * pow(digSum, a) + c; int temp = solVal; int sum = 0; while (temp) { sum += temp % 10; temp /= 10; } if (sum == digSum && solVal < 1e9) solutionCount++; } return solutionCount; } int main(){ int a = 3, b = 6, c = 4; cout<<"The number of solutions of the equations is "<<countSolutions(a, b, c); return 0; }
เอาต์พุต
The number of solutions of the equations is 3