15 lines of code AC-Link/Cut Tree CodeForces-614A (explosive long long processing + quick power explanation)

Inspirational use less code for efficient expression


Problem describe

Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure.
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 l, r 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 l, r 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).


Thematic analysis

Question : Input l, r, k, and output all the powers of k in the interval [l, r].
Such as: input 1, 10, 2 output 1 2 4 8

Analysis : Needless to say about exponentiation, pow() , cumulative multiplication , and fast exponentiation are all acceptable.
But in the process of solving, there will be long long bursts, such as: k to the power of n = 10^18, k=1000, if you continue to calculate at this time, 10^21the number of will appear . Exceeding the value range of long long, even unsigned long long cannot be stored.

The solution is: multiplication changes division.

Such as: change the formula if(pow(k, i) <= r )to: if(pow(k,i-1) <= r/k)so that the value can be taken within the range of long long.

Two AC codes are listed below. Code one is fast exponentiation method , and code two is simple method .

If you don’t know much about fast power, please go to step——> Explain the basic idea of ​​fast power


Code One (Quick Power Method)

#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
ll quick_power(ll a, ll q) {
    
    
	ll sum = 1;
	while(q) {
    
    
		if(q%2==1) sum *= a;
		a*=a;
		q /= 2;
	}
	return sum;
}
int main( ) {
    
    
	ll l, r, k; cin>>l>>r>>k;
	bool flag = true;
	for(ll i = 0; quick_power(k, i)<=r ; i++) {
    
    
		ll num = quick_power(k,i);
		if(num>=l) {
    
    
			cout <<(flag?"":" ")<< num;
			flag = false;
		}
		if(num > r/k) break;
	}
	if(flag) cout << -1;
return 0; }

Code two (naive approach)

#include<iostream>
#include<cmath>
using namespace std;
typedef unsigned long long llu;
int main( ) {
    
    
	llu l, r, k; cin>>l>>r>>k;
	llu t = 1, flag=0;
	while(t <= r) {
    
    
		if(t>=l) {
    
     cout << t << ' '; flag = 1; }
		if(t>r/k) break;
		t*=k; 
	}
	if(!flag) cout << -1;
return 0; }

This world is that some people are always working day and night, while others find that the world has changed when they wake up.

Guess you like

Origin blog.csdn.net/weixin_43899069/article/details/108319179