[CodeForces - 614B] B - Gena's Code

A - Link/Cut Tree

Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the exposeprocedure.

Unfortunately, Rostislav is unable to understand the definition of this procedure, so he decided to ask programmer Serezha to help him. Serezha agreed to help if Rostislav solves a simple task (and if he doesn‘t, then why would he need Splay trees anyway?)

Given integers lr and k, you need to print all powers of number k within range from l to r inclusive. However, Rostislav doesn‘t want to spent time doing this, as he got interested in playing a network game called Agar with Gleb. Help him!

Input

The first line of the input contains three space-separated integers lr and k (1?≤?l?≤?r?≤?1018, 2?≤?k?≤?109).

Output

Print all powers of number k, that lie within range from l to r in the increasing order. If there are no such numbers, print "-1" (without the quotes).

Example

Input

1 10 2

Output

1 2 4 8 

Input

2 4 5

Output

-1

Note

Note to the first sample: numbers 20?=?1, 21?=?2, 22?=?4, 23?=?8 lie within the specified range. The number 24?=?16 is greater then 10, thus it shouldn‘t be printed.

题目意思是,给你一个区间和一个数,求有几个这个数的正整数次幂在这个区间内.

由于数据很大,在判断是否超出R的时候可能会爆long long.所以在判断时不能用乘,而应该用除,这样就不会爆了.

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 long long L,R,K;
 5 int main(){
 6     scanf("%lld%lld%lld",&L,&R,&K);
 7     long long x=1; bool fl=0;
 8     if (L<=x&&x<=R){printf("1 "); fl=1;}
 9     for (int i=1; R/x>=K; i++){
10         x*=K; if (x>=L&&x<=R){printf("%lld ",x); fl=1;}
11     }
12     if (fl==0) puts("-1");
13     return 0;
14 }

[CodeForces - 614B] B - Gena's Code

时间: 2024-10-02 23:30:00

[CodeForces - 614B] B - Gena's Code的相关文章

CodeForces 614B Gena&#39;s Code

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <math.h> using namespace std; int flag; char s[100000+10]; int zero; char q[100000+10]; bool Perfect() { int len=strlen(s); for(int i=1;

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f

Codeforces 417D Cunning Gena(状态压缩dp)

题目链接:Codeforces 417D Cunning Gena 题目大意:n个小伙伴.m道题目,每一个监视器b花费,给出n个小伙伴的佣金,所须要的监视器数,以及能够完毕的题目序号. 注意,这里仅仅要你拥有的监视器数量大于小伙伴须要的监视器数量就可以. 求最少花费多少金额能够解决全部问题. 解题思路:dp[i],i为一个二进制数.表示完毕这些题目的最小代价,可是这里要注意,由于有个监视器的数量.普通情况下要开一个二维的状态.可是2^20次方有一百万,再多一维的数组会超内存,所以我的做法是将每一

Gena&#39;s Code

 Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, fin

COdeforces#417D Cunning Gena(状压DP)

A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his n friends that they will solve the problems for him. The participants

codeforces 614B(div.2) 模拟

模拟乘法 有毒的一题,各种细节..代码写得自己都不想看.. #include"cstdio" #include"queue" #include"cmath" #include"stack" #include"iostream" #include"algorithm" #include"cstring" #include"queue" #includ

【Codeforces 1163 D】Mysterious Code

题意:给一个带有通配符的字符串,以及两个匹配串,要求把这个字符串补全后第一个匹配串出现次数减去第二个出现次数最大.求这个差的最大值. 思路:首先肯定是构造AC自动机. 然后在第一个串结尾的节点处放上1,第二个串结尾处放上-1,就变成了把字符串跑遍之后每一次加上这个节点以及所有\(fail\)的值得到的和最大. 那么就是个在AC自动机上\(dp\)的题. 考虑\(dp(i,j)\)表示现在到了第\(i\)位,跑到了AC自动机的\(j\)号节点,然后转移: 如果这个位置不是通配符,那么就沿着应该走的

Codeforces Round #289 Div2 E

Problem 给一串长度为N的字符串,对于每个字符,若字符为元音,则权值为1,否则为0.一个子串的权值定义为该串所有字符权值之和除以字符个数,一个母串的权值定义为所有子串的权值之和.求母串的权值. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 N: [1, 5*10^5] 字符集: 'A'-'Z' 元音: I E A O U Y Solution 考虑每个元音字符对母串的贡献,可以找出规律. More 举"ABCDOEFGHKMN"

Codeforces Round #290 Div1 A

Problem 给N串字符串Si,通常定义字典序大小关系为 'a'<'b'<'c'<......<'y'<'z',现要求重新定义大小关系使得对于任意 i,j(i<j)满足Si <Sj,输出大小关系(一串'a'-'z'的排列),或者输出不存在(任意大小关系都不能满足要求). Limits Time Limit(ms): 2000 Memory Limit(MB): 256 N: 100 |Si|: 100 Solution 用图论方法解决,发现满足拓扑关系.枚举相邻