Thursday 27 October 2016

Spoj SPCQ

LOGIC:

You should need to convince yourself that after some iterations surely you will get your answer.
Now here question is why?
Answer of this question is that there can be maximum 18-19 digits that mean maximum number
can be '9' 18-19 times so sum of this will be 9 * 19 = 171 maximum, so we can be sure that if  we
start increase a number from Number + 1,  Number + 2, .... Number + 171 so at most 171 iteration will be required to get answer :)
     
Below you can see code also


#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = 1<<29;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
template<class T> void chmax(T & a, const T & b) { a = max(a, b); }
template<class T> void chmin(T & a, const T & b) { a = min(a, b); }
/////////////////////////////////////////////////////////////////////

bool SUM_OF_DIG(unsigned long long num) {
unsigned long long temp = num;
int sum = 0;
while(num > 0) {
sum += (num%10);
num /= 10;
}
return (temp%sum == 0);
}

int main()
{
int T;
scanf("%d", &T);

while(T--) {
unsigned long long N;
scanf("%llu", &N);
for(; ; ) {
if(SUM_OF_DIG(N)) {
printf("%llu\n", N);
break;
}
N++;
}
}
    return 0;
}


happy coding :)

No comments:

Post a Comment