SRM 150 1A 2013.12.3
DIV 1
250
Problem Statement
The digits 3 and 9 share an interestingproperty. If you take any multiple of 3 and sum its digits, you get anothermultiple of 3. For example, 118*3 = 354 and 3+5+4 = 12, which is a multiple of3. Similarly, if you take any multiple of 9 and sum its digits, you get anothermultiple of 9. For example, 75*9 = 675 and 6+7+5 = 18, which is a multiple of9. Call any digit for which this property holds interesting, except for 0 and1, for which the property holds trivially.
A digit that is interesting in one base isnot necessarily interesting in another base. For example, 3 is interesting inbase 10 but uninteresting in base 5. Given an int base, your task is to returnall the interesting digits for that base in increasing order. To determinewhether a particular digit is interesting or not, you need not consider allmultiples of the digit. You can be certain that, if the property holds for allmultiples of the digit with fewer than four digits, then it also holds formultiples with more digits. For example, in base 10, you would not need toconsider any multiples greater than 999.
Definition
Class:
InterestingDigits
Method:
digits
Parameters:
int
Returns:
vector <int>
Method signature:
vector <int> digits(int base)
(be sure your method is public)
Notes
-
When base is greater than 10, digits mayhave a numeric value greater than 9. Because integers are displayed in base 10by default, do not be alarmed when such digits appear on your screen as morethan one decimal digit. For example, one of the interesting digits in base 16is 15.
Constraints
-
base is between 3 and 30, inclusive.
Examples
0)
10
Returns: { 3, 9 }
All other candidate digits fail forbase=10. For example, 2 and 5 both fail on 100, for which 1+0+0=1. Similarly, 4and 8 both fail on 216, for which 2+1+6=9, and 6 and 7 both fail for 126, forwhich 1+2+6=9.
1)
3
Returns: { 2 }
2)
9
Returns: { 2, 4, 8 }
3)
26
Returns: { 5, 25 }
4)
30
Returns: { 29 }
This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
177.18
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class InterestingDigits
{
public:vector <int> digits(int base)
{
vector<int> ans;
ans.clear();
int x[5];
bool flag;
for(int i=2;i<base;i++)
{
flag=true;
for(int j=1;j<4;j++) x[j]=0;
x[0]=i;
while ((x[3]==0)&&(flag))
{
x[0]+=i;
for(int k=1;k<4;k++)
{
x[k]+=x[k-1]/base;
x[k-1]%=base;
}
int temp=0;
for(int e=0;e<4;e++)
{
temp+=x[e];
}
if (temp%i!=0) flag=false;
}
if (flag) ans.push_back(i);
}
return ans;
}
};