Educational Codeforces Round 66 (Rated for Div. 2) A
A. From Hero to Zero
题目链接:http://codeforces.com/contest/1175/problem/A
题目
ou are given an integer n and an integer k
In one step you can do one of the following moves:
decrease n by 1;
divide n by k if n is divisible by k.
For example, if n=27 and k=3 you can do the following steps: 27→26→25→24→8→7→6→2→1→0.
You are asked to calculate the minimum number of steps to reach 0 from n.
input
The first line contains one integer t (1≤t≤100) — the number of queries.
The only line of each query contains two integers n
and k (1≤n≤1018, 2≤k≤1018).
output
For each query print the minimum number of steps to reach 0
from n in single line
Example
intput
2
59 3
1000000000000000000 10
output
8
19
题意
给你两个数n,k,你需要将n每次经过以下两个**步骤之一**从而得到0,输出变换次数:
要么n=n-1,要么将n=n/k(前提n能整除k)。
思路
范围太大,暴力绝对TLE,尝试就是浪费生命!
巧办法:
n%k!=0时,变换的步数就是n%k的值,此时当前n=(n-减掉该步数)
n%k==0时,变换的步数就是1,此时当前n=n/k,
n为0结束,将步数累加输出即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+;
int main()
{
int T;
cin>>T;
while(T--) {
ll n, k;
cin >> n >> k;
ll result = ;
while (n != ) {
ll book = n % k;
if (book != ) {
result += book;
n = n - book;
} else {
result += ;
n = n / k;
}
}
cout << result << endl;
}
return ;
}
Educational Codeforces Round 66 (Rated for Div. 2) A的更多相关文章
- Educational Codeforces Round 66 (Rated for Div. 2) B. Catch Overflow!
链接:https://codeforces.com/contest/1175/problem/B 题意: You are given a function ff written in some bas ...
- Educational Codeforces Round 66 (Rated for Div. 2) A. From Hero to Zero
链接:https://codeforces.com/contest/1175/problem/A 题意: You are given an integer nn and an integer kk. ...
- Educational Codeforces Round 66 (Rated for Div. 2)
A.直接模拟. #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
随机推荐
- 微信公众平台中临时二维码的scene_id为32位非0整型
原文:微信公众平台中临时二维码的scene_id为32位非0整型 微信公众平台中临时二维码的scene_id为32位非0整 ...
- 上传图片时,使用GDI+中重绘方式将CMYK图片转为RGB图片
原文:上传图片时,使用GDI+中重绘方式将CMYK图片转为RGB图片 我们知道,如果网站上传图片时,如果用户上传的是CMYK图片,那么在网站上将是无法显示的,通常的现象是出现一个红叉.下面使用将Ima ...
- Matlab Tricks(二十五) —— 二维图像的 shuffle
比如对于 mnist (手写字符图像),每幅图像的像素点为 28*28,所以有: perm = randperm(28*28); % 重排列 mnist.train_images = reshape( ...
- ATS项目更新(3) 远程同步到执行机器
1: echo %time% 2: 3: 4: rem ** ipc and mapping 5: c: 6: net use x: /del 7: net use y: /del 8: net us ...
- 微信小程序--实现图片上传
前端: 微信开发者工具 后端:.Net 服务器:阿里云 这里介绍微信小程序如何实现上传图片到自己的服务器上 前端代码 data: { productInfo: {} }, //添加Banner bin ...
- vc有关 directx组态,和dxsdk_extras(directshow)
2009-11-10 0:28 此文章:自己编写 转载于<汤姆&杰瑞> DirectShow 1 -- 下载与VC配置 1 DirectX SDK9 Directshow sd ...
- WPF GridSplitter最好设置HorizontalAlignment和VerticalAlignment,否则不可以左右移动
<Window x:Class="XamlTest.Window5" xmlns="http://schemas.microsoft.com/winf ...
- PHP命名空间和自动加载初探
参考资料: PHP手册-语言参考:http://php.net/manual/zh/language.namespaces.php 概要: 1. 声明了命名空间之后,下面的const, funct ...
- 零元学Expression Blend 4 – Chapter 43 如何指定Childwindow PopUp位置
原文:零元学Expression Blend 4 – Chapter 43 如何指定Childwindow PopUp位置 有网友询问我有关Childwindow是否能指定弹出位置? 其实只要透过小小 ...
- 如何作为一个优秀的ERP实施顾问
原文地址:如何作为一个优秀的ERP实施顾问作者:天思软件 作一个优秀的ERP实施顾问无论是天思.金蝶,用友,还是其他业务软件.实施顾问的发展道路都差不多. 1.初级实施顾问,中级实施顾问,高级实施顾问 ...