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 ...
随机推荐
- 局部QEventLoop帮助QWidget不消失(也就是有一个局部事件循环始终在运行,导致程序被卡住那里,但仍可以接受事件。说白了就是有一个while语句死活不肯退出,直到收到退出信号)
熟悉的陌生人 Qt 是事件驱动的,所以当你用Qt的时候,几乎时时刻刻和 QEventLoop 打交道.,只是你可能没有意识到: QCoreApplicaton::exec() QApplication ...
- WPF 实现跑马灯效果的Label控件,数据绑定方式实现
原文:WPF 实现跑马灯效果的Label控件,数据绑定方式实现 项目中需要使用数据绑定的方式实现跑马灯效果的Label,故重构了Label控件:具体代码如下 using System; using S ...
- ATS项目更新(1) CC视图与备份路径同步
1: subst t: /d 2: subst t: D:\PublicViews\Automation_Framework\SQA_ATE_DEV 3: 4: rem ** update folde ...
- 大数据_zookeeper环境搭建中的几个坑
文章目录 [] Zookeeper简介 关于zk的介绍, zk的paxos算法, 网上已经有各位大神在写了, 本文主要写我在搭建过程中的几个极有可能遇到的坑. Zookeeper部署中的坑 坑之一 E ...
- 通过.NET客户端调用Web API(C#)
3.2 Calling a Web API From a .NET Client (C#) 3.2 通过.NET客户端调用Web API(C#) 本文引自:http://www.asp.net/web ...
- C# WebApi使用AttributeRoutes特性路由
1.在创建WebApi中默认的路由规则,只能满足一般简单的RESTful风格,如 api/Products/{id}. 但是在实际运用中很难严格满足RESTful要求的WebApi.因此需要使用高版本 ...
- Web service的学习资源
看了半天的Web service,总算是对它有了一点眉目,不枉此行:)那就整理一下吧,来日还需要用到呢! 1.什么是Web service(请看这儿). 2.Web service的开发 ...
- MQTT开源代理Mosquitto源码分析(访问控制篇)
一.整体流程概览 从GitHub下载源码后,代理的源码在src中,同时还用到了lib库中的一些函数.对项目的工作流程有个大概理解是分析mosquitto的访问控制权限的基础,网络上已有很多中文博客在介 ...
- PySide——Python图形化界面入门教程(一)
PySide——Python图形化界面入门教程(一) ——基本部件和HelloWorld 翻译自:http://pythoncentral.io/intro-to-pysidepyqt-basic-w ...
- win7 64 下安装MyGeneration 遇到的问题解决方法
win7 64 下安装MyGeneration 遇到的问题 ---------------------------MyGeneration 1.3 Setup-------------------- ...