{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# [Chef and Strings](https://www.codechef.com/JULY20B/problems/CHEFSTR1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Having already mastered cooking, Chef has now decided to learn how to play the guitar. Often while trying to play a song, Chef has to skip several strings to reach the string he has to pluck. Eg. he may have to pluck the 1st string and then the 6th string. This is easy in guitars with only 6 strings; However, Chef is playing a guitar with 106 strings. In order to simplify his task, Chef wants you to write a program that will tell him the total number of strings he has to skip while playing his favourite song.\n", "\n", "![fig1](https://codechef_shared.s3.amazonaws.com/download/Images/JULY20/CHEFSTR1/CHEFSTR1.png)\n", "\n", "This is how guitar strings are numbered (In ascending order from right to left). Eg. to switch from string 1 to 6, Chef would have to skip 4 strings (2,3,4,5).\n", "\n", "***Input:***\\\n", "First line will contain T, number of testcases. Then the testcases follow.\\\n", "The first line of each test case contains N, the number of times Chef has to pluck a string\\\n", "The second line of each test case contains N space separated integers - S1, S2, …, SN, where Si is the number of the ith string Chef has to pluck.\\\n", "***Output:***\\\n", "For each testcase, output the total number of strings Chef has to skip over while playing his favourite song.\n", "\n", "***Constraints***\\\n", "1≤T≤10\\\n", "2≤N≤105\\\n", "1≤Si≤106\\\n", "For each valid i, Si≠Si+1\\\n", "Subtasks\\\n", "30 points : for each valid i, Si0:\n", " r=n%10\n", " sum_n+=r\n", " n//=10\n", " return sum_n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "digit_sum(324)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ " 2\n", " 3\n", " 10 4\n", " 8 12\n", " 7 6\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "0 2\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ " 2\n", " 5 10\n", " 3 4\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "2 1\n" ] } ], "source": [ "for _ in range(int(input())):\n", " c1=0\n", " c2=0\n", " for i in range(int(input())):\n", " n1,n2=input().split()\n", " if digit_sum(int(n1))>digit_sum(int(n2)):\n", " c1+=1\n", " elif digit_sum(int(n1))c2:\n", " print('0',c1)\n", " elif c10:\n", " m-=n2\n", " n2*=2\n", " while m>0:\n", " c+=1\n", " print(m)\n", " if (m-n2)>0:\n", " m-=n2\n", " n2*=2\n", " else:\n", " break\n", " print(c)\n", " else:\n", " print(c)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 4 }