Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
#include <bits/stdc++.h>
using namespace std;
int sum(long long x)
{
int ans = 0;
while (x)
ans += x % 10, x /= 10;
return ans;
}
int main()
{
int t;
cin >> t;
while (t--)
{
long long n, d;
cin >> n >> d;
map<long long, int> mp;
mp[n] = 0;
queue<long long> q;
q.push(n);
for (int i = 1; i <= 10000; ++i)
{
long long x = q.front(); q.pop();
// cout << x << ' ';
int step = mp[x];
if (mp.find(x + d) == mp.end())
{
mp[x + d] = mp[x] + 1;
q.push(x + d);
}
x = sum(x);
if (mp.find(x) == mp.end())
{
mp[x] = step + 1;
q.push(x);
}
}
for (auto x : mp) {
cout << x.first << ' ' << x.second << '\n';
break;
}
}
}
# cook your dish here
# cook your dish here
def T(N):
A=str(N)
n=len(A)
y=0
while (n!=1):
x=0
for j in A:
x+=int(j)
A=str(x)
n=len(A)
y+=1
return (int(A), y)
def U(N):
B=str(N)
x=0
for j in B:
x+=int(j)
return x
a=int(input())
for i in range(a):
N, D=map(int, input().split())
p=T(N)
q=T(D)
ans=[0, 0]
if q[0]==9:
ans[0]=p[0]
if p[1]<=T(N+D)[1]+1:
ans[1]=p[1]
else:
ans[1]=T(N+D)[1]+1
elif q[0]==6:
a=min(p[0], T(p[0]+6)[0], T(p[0]+3)[0])
ans[0]=a
if a==p[0]:
ans[1]=p[1]
elif a==T(p[0]+6)[0]:
ans[1]=min(T(U(N)+D)[1]+2, T(N+D)[1]+1)
else:
ans[1]=min(T(U(N)+2*D)[1]+3, T(U(N+D)+D)[1]+3, T(N+2*D)[1]+2)
elif q[0]==3:
a=min(p[0], T(p[0]+6)[0], T(p[0]+3)[0])
ans[0]=a
if a==p[0]:
ans[1]=p[1]
elif a==T(p[0]+3)[0]:
ans[1]=min(T(U(N)+D)[1]+2, T(N+D)[1]+1)
else:
ans[1]=min(T(U(N)+2*D)[1]+3, T(U(N+D)+D)[1]+3, T(N+2*D)[1]+2)
else:
g=0
ans[0]=1
A=N
for j in range(20):
if T(A)[0]==1:
break
else:
A=A+D
g+=1
ans[1]=T(N+g*D)[1]+g
for j in range(0, g+1):
if T(U(N+(j*D))+(g-j)*D)[1]+g+1<ans[1]:
ans[1]=T(U(N+(j*D))+(g-j)*D)[1]+g+1
print(ans[0], ans[1])
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <limits.h>
long long int min,hcount;
long long int digitsum(long long int x)
{
long long int temp=x,dsum=0;
while(temp>0)
{
dsum=dsum+temp%10;
temp=temp/10;
}
return dsum;
}
void solve(long long int n,long long int d,long long int count)
{
if(n<min)
{
min=n;
hcount=count;
}
else if(n==min)
{
if(hcount>count)
hcount=count;
}
if(count>=15)
return;
solve(n+d,d,count+1);
solve(digitsum(n),d,count+1);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long int n,d;
scanf("%lld %lld",&n,&d);
long long int count=0;
min=n;
hcount=n;
solve(n,d,count);
printf("%lld %lld\n",min,hcount);
}
}
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader()throws IOException{
br=new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s)throws IOException{
br=new BufferedReader(new FileReader(new File(s)));
}
String next(){
while(st==null || !st.hasMoreElements()){
try{
st=new StringTokenizer(br.readLine());
}
catch(Exception e){
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine(){
String str="";
try{
str=br.readLine();
}
catch(Exception e){
e.printStackTrace();
}
return str;
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
}
public static long digisum(long a){
long sum=0;
while(a!=0){
sum+=(a%10);
a/=10;
}
return sum;
}
public static long digitsum(long a){
return (a%9==0)?9:a%9;
}
public static void main(String args[]) throws IOException {
FastReader in = new FastReader();
PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out));
int T=in.nextInt();
while(T-->0){
HashSet<Long>hs=new HashSet<>();
long n=in.nextLong();
long d=in.nextLong();
long dup=n;
while(true){
long p=digitsum(n);
if(!hs.contains(p))
hs.add(p);
else
break;
n=n+d;
}
long ans=Collections.min(hs);
Queue<Long>q=new LinkedList<>();
int ops=0; n=dup;
q.add(n);q.add(null);
while(!q.isEmpty()){
if(q.peek()==null){
q.remove();
ops++;
if(!q.isEmpty())
q.add(null);
}
else{
long a=q.peek();
q.remove();
if(a==ans) break;
if(a>=10)
q.add(digisum(a));
q.add(a+d);
}
}
out.println(ans+" "+ops);
}
out.close();
}
}
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
t=int(input())
qw=100
def digitsum(n):
s=0
while n>0:
s=s+n%10
n=n//10
return s
def calcu(l,n,arr,d):
c1=n+d
if c1!=n:
arr.append(c1)
c2=digitsum(n)
if c2!=n:
arr.append(c2)
global m
global step
if min(c1,c2)<m:
m=min(c1,c2)
step=l+1
ul=100
for i in range(100):
ul=ul+100
for i in range(t):
n,d=[int(i) for i in input().strip().split()]
l=0
step=0
m=n
array1=[n,]
array2=[]
while l<11:
for i in array1:
calcu(l,i,array2,d)
l=l+1
array1=array2.copy()
array2=[]
if m==1:
break
ans=str(m)+" "+str(step)
print(ans)
for i in range(100):
qw=qw+100
def sumdig(n):
r = 0
while n > 0:
r += n%10
n = n/10
# endwhile
return r
# end fun
t = int(raw_input())
for i in range(t):
st = raw_input().split()
N = int(st[0])
D = int(st[1])
L = [[N,0]]
S = set()
S.add(N)
mi = N
nop = 0
p = 0
vn = 0
while vn < 16:
v = L[p][0]
vn = L[p][1]
n = sumdig(v)
if not (n in S):
S.add(n)
L.append([n,vn+1])
if n < mi:
mi = n
nop = vn+1
# endif
# endif
n = v+D
if not (n in S):
S.add(n)
L.append([n,vn+1])
# endif
p += 1
# endwhile
print mi, nop
# endfor i
using System;
using System.Collections.Generic;
using System.Linq;
namespace CodeChef.Oct18.MINDSUM
{
#region ConsoleHelper
public interface IConsoleHelper
{
string ReadLine();
T ReadLineAs<T>();
string[] ReadLineAndSplit(char delimiter = ' ');
List<T> ReadLineAndSplitAsListOf<T>(char delimiter = ' ');
void WriteLine(object obj);
void Debug(object obj);
}
public class ConsoleHelper : IConsoleHelper
{
public virtual string ReadLine()
{
return Console.ReadLine();
}
public T ReadLineAs<T>()
{
var line = this.ReadLine();
return ConvertTo<T>(line);
}
public string[] ReadLineAndSplit(char delimiter = ' ')
{
var splittedLine = this.ReadLine().Split(delimiter);
return splittedLine;
}
public List<T> ReadLineAndSplitAsListOf<T>(char delimiter = ' ')
{
var splittedLine = this.ReadLineAndSplit();
return splittedLine.Select(ConvertTo<T>).ToList();
}
public virtual void WriteLine(object obj)
{
Console.WriteLine(obj);
}
public void Debug(object obj)
{
Console.Error.WriteLine(obj);
}
private static T ConvertTo<T>(string value)
{
return (T) Convert.ChangeType(value, typeof(T));
}
}
#endregion
public static class Program
{
public static IConsoleHelper ConsoleHelper;
static Program()
{
ConsoleHelper = new ConsoleHelper();
}
public static void Main(string[] args)
{
SolveMultiple();
}
public static void SolveMultiple()
{
var t = ConsoleHelper.ReadLineAs<int>();
for (var k = 0; k < t; k++)
{
Solve();
}
}
private static void Solve()
{
var input = ConsoleHelper.ReadLineAndSplitAsListOf<long>();
var n = input[0];
var d = input[1];
var min = Solve(n, d);
ConsoleHelper.WriteLine(min);
}
private static Node Solve(long n, long d)
{
var firstNode = new Node(n, 0, 0);
var nodes = new HashSet<Node> { firstNode };
var queue = new Queue<Node>();
queue.Enqueue(firstNode);
while (queue.Count > 0)
{
var node = queue.Dequeue();
if (node.Value == 1)
return node;
var op1 = new Node(ToDigitsSum(node.Value), node.OpCount + 1, node.AddOpCount);
if(!nodes.Contains(op1))
{
nodes.Add(op1);
queue.Enqueue(op1);
}
var op2 = new Node(node.Value + d, node.OpCount + 1, node.AddOpCount+1);
if (op2.AddOpCount < 9 && !nodes.Contains(op2))
{
nodes.Add(op2);
queue.Enqueue(op2);
}
}
var min = nodes.Min();
return min;
}
private static long ToDigitsSum(long number)
{
var sum = number.ToString().Sum(c => c - '0');
return sum;
}
}
public class Node : IComparable
{
public Node(long value, int opCount, int addOpCount)
{
Value = value;
OpCount = opCount;
AddOpCount = addOpCount;
}
public long Value { get; set; }
public int OpCount { get; set; }
public int AddOpCount { get; set; }
public override bool Equals(object obj)
{
var node = obj as Node;
return node != null && Value == node.Value;
}
public override int GetHashCode()
{
return -1937169414 + Value.GetHashCode();
}
public int CompareTo(object obj)
{
var node = (Node)obj;
var compareTo = this.Value.CompareTo(node.Value);
if (compareTo == 0)
compareTo = this.OpCount.CompareTo(node.OpCount);
return compareTo;
}
public override string ToString()
{
return $"{Value} {OpCount}";
}
}
}
function digitsum(x) {
var sum = 0;
while (x) {
sum += x % 10;
x = Math.floor(x / 10);
}
return sum;
}
function sum(d) {
return function (n) {
return n + d;
}
}
function node(value, add, depth, root) {
if(depth != 0 && value == root){
return null;
} else if (depth > 20) {
return null;
} else {
return {
value: value,
depth: depth,
right: value < 10? null : node(digitsum(value), add, depth + 1, root),
left: node(add(value), add, depth + 1, root)
}
}
}
function min(node) {
if (!node)
return { value: Number.MAX_SAFE_INTEGER }
if(node.value == 1)
return node
var result = node
var right = min(node.right)
var left = min(node.left)
if (left.value < result.value) {
result = left
} else if (left.value == result.value) {
if (left.depth < result.depth)
result = left
}
if (right.value < result.value) {
result = right
} else if (right.value == result.value) {
if (right.depth < result.depth)
result = right
}
return {
value: result.value,
depth: result.depth
}
}
var input = require('readline').createInterface({
input: process.stdin,
terminal: false
})
input.on('line', function(line){
var p = line.split(' ')
if(!p[1]) return;
var result = min(node(+p[0], sum(+p[1]), 0, +p[0]))
console.log(`${result.value} ${result.depth}`)
})
package main
import (
"bufio"
"fmt"
"os"
)
func readInt(bytes []byte, from int, val *int) int {
i := from
sign := 1
if bytes[i] == '-' {
sign = -1
i++
}
tmp := 0
for i < len(bytes) && bytes[i] != ' ' {
tmp = tmp*10 + int(bytes[i]-'0')
i++
}
*val = tmp * sign
return i
}
func readNum(scanner *bufio.Scanner) (a int) {
scanner.Scan()
readInt(scanner.Bytes(), 0, &a)
return
}
func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
res := readNNums(scanner, 2)
a, b = res[0], res[1]
return
}
func readNNums(scanner *bufio.Scanner, n int) []int {
res := make([]int, n)
x := 0
scanner.Scan()
for i := 0; i < n; i++ {
for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
x++
}
x = readInt(scanner.Bytes(), x, &res[i])
}
return res
}
func fillNNums(scanner *bufio.Scanner, n int, res []int) {
x := 0
scanner.Scan()
for i := 0; i < n; i++ {
for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
x++
}
x = readInt(scanner.Bytes(), x, &res[i])
}
}
func readUint64(bytes []byte, from int, val *uint64) int {
i := from
var tmp uint64
for i < len(bytes) && bytes[i] != ' ' {
tmp = tmp*10 + uint64(bytes[i]-'0')
i++
}
*val = tmp
return i
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
tc := readNum(scanner)
for tc > 0 {
tc--
var n, d uint64
scanner.Scan()
pos := readUint64(scanner.Bytes(), 0, &n)
readUint64(scanner.Bytes(), pos+1, &d)
ans, cnt := solve(int64(n), int64(d))
fmt.Printf("%d %d\n", ans, cnt)
}
}
func sumOfDigit(x int64) int64 {
r := x % 9
if r == 0 {
return 9
}
return r
}
func solve(n, d int64) (int, int) {
ans := n
x := n
if n > 9 {
ans = sumOfDigit(n)
x = ans
}
for i := 0; i < 12; i++ {
x += d
if x > 9 {
x = sumOfDigit(x)
if x < ans {
ans = x
}
}
}
return int(ans), count(n, d, ans, 0)
}
func count(n, d, min int64, c int) int {
if c > 12 {
return c
}
if n == min {
return c
}
k1 := count(n+d, d, min, c+1)
k2 := count(sum(n), d, min, c+1)
if k1 < k2 {
return k1
}
return k2
}
func sum(n int64) int64 {
var res int64
for n > 0 {
res += n % 10
n /= 10
}
return res
}
In our experience, we suggest you solve this Minimize Digitsum CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Minimize Digitsum CodeChef Solution.
I hope this Minimize Digitsum CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.
Keep Learning!