codeparrot/codecomplex huggingface dataset:
// Problem: 338_A. Quiz
// Time complexity: logn
import java.util.*;
import java.io.*;

public class Template {
	public static void main(String[] args) throws IOException {
		st = new StringTokenizer(rd.readLine());
		n = Long.parseLong(st.nextToken());
		m = Long.parseLong(st.nextToken());
		k = Long.parseLong(st.nextToken());
		long s = n - m;
		s = Math.min(s, m / (k - 1));
		s = Math.min(s, n / k);
		long score = 0;
		score = (s * (k - 1))%P;
		long n1 = n - k * s, m1 = m - (k - 1) * s;
		sc = 0;
	//	rec(n, m);
	//	System.out.println(sc);
		if (m1 == n1) {
			score = (score + full(m1)) % P;
			System.out.println(score);
			return;
		}
			score = (score + m1) % P;
			System.out.println(score);
	}

	static long full(long N) {
		long x = N / k, r = N - x * k;
		long powTwo = powMod(2, x + 1) - 2 + 2*P;
		powTwo %= P;
		powTwo = (powTwo * k) % P;
		powTwo = (powTwo + r) % P;
		return powTwo;
	}
	
	static long sc = 0;
	
	static void rec(long N, long M){
		if(N==M){ sc = (sc + full(N))%P; return; }
		if(N>=k && M>=k-1){
			sc = (sc + (k-1))%P;
			rec(N-k, M-(k-1));
			return;
		}
		sc = (sc + M)%P;
	}

	static long powMod(long a, long p) {
		if (p == 0)
			return 1L;
		long h = powMod(a, (p >> 1));
		h = (h * h) % P;
		return p % 2 == 0 ? h : (a * h) % P;
	}

	static long n, m, k;

	static long P = 1000000009;

	static StringTokenizer st;
	static BufferedReader rd = new BufferedReader(new InputStreamReader(
			System.in));
	static PrintWriter pw = new PrintWriter(System.out);

}
// Problem: 338_A. Quiz
// Time complexity: logn
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class Quiz {
    public static long pow(long base, long power) {
        if (power == 0)
            return 1;
        long half = pow(base, power / 2) % mod;
        half *= half;
        half %= mod;
        if (power % 2 == 1)
            half *= base;
        return half % mod;
    }

    static long mod = (long) (1e9 + 9);

    public static void main(String[] args) {
        InputReader r = new InputReader(System.in);
        int n = r.nextInt();
        int m = r.nextInt();
        int k = r.nextInt();
        int buckets = n / k;
        int rem = n - buckets * k;
        long low = 0, high = buckets, itr = 30;
        while (itr-- > 0) {
            long mid = (low + high) / 2;
            long correct = mid * k + rem + (buckets - mid) * (k - 1);
            if (correct < m)
                low = mid;
            else
                high = mid;
        }
        long pow = (pow(2, high + 1) - 2 + mod) % mod;
        pow *= k;
        pow %= mod;
        long res = m - (high * k) + pow + 10 * mod;
        res %= mod;
        System.out.println(res);
    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public InputReader(FileReader stream) {
            reader = new BufferedReader(stream);
            tokenizer = null;
        }

        public String nextLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }
    }
}
// Problem: 338_A. Quiz
// Time complexity: logn
import java.util.*;
import java.io.*;
public class a {
    static long mod = 1000000009;
    static ArrayList<Integer>[] g;
public static void main(String[] args) throws IOException
{
    //Scanner input = new Scanner(new File("input.txt"));
    //PrintWriter out = new PrintWriter(new File("output.txt"));
  input.init(System.in);
  PrintWriter out  = new PrintWriter((System.out));
  
  int n = input.nextInt(), m = input.nextInt(), k = input.nextInt();
  long border = n-n/k;
  if(m<=border) out.println(m);
  else
  {
      long count = m- border;
      long first = ((pow(2, count+1) + mod - 2)*k)%mod;
      first += m - k*count;
      out.println(first%mod);
  }
  
  out.close();
}
static long pow(long x, long p)
{
    if(p==0) return 1;
    if((p&1) > 0)
    {
        return (x*pow(x, p-1))%mod;
    }
    long sqrt = pow(x, p/2);
    return (sqrt*sqrt)%mod;
}
static long gcd(long a, long b)
{
    if(b==0) return a;
    return gcd(b, a%b);
}
static class input {
  static BufferedReader reader;
  static StringTokenizer tokenizer;

  /** call this method to initialize reader for InputStream */
  static void init(InputStream input) {
      reader = new BufferedReader(
                   new InputStreamReader(input) );
      tokenizer = new StringTokenizer("");
  }

  /** get next word */
  static String next() throws IOException {
      while ( ! tokenizer.hasMoreTokens() ) {
          //TODO add check for eof if necessary
          tokenizer = new StringTokenizer(
                 reader.readLine() );
      }
      return tokenizer.nextToken();
  }

  static int nextInt() throws IOException {
      return Integer.parseInt( next() );
  }
  
  static double nextDouble() throws IOException {
      return Double.parseDouble( next() );
  }
  static long nextLong() throws IOException {
      return Long.parseLong( next() );
  }
  static String nextLine() throws IOException {
    return reader.readLine();
  }
}
}
// Problem: 338_A. Quiz
// Time complexity: logn
import java.util.List;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author RiaD
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		Reader in = new Reader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskA {
    private static final long MOD = 1000000009;

    public void solve(int testNumber, Reader in, OutputWriter out) {
        long answer = 0;
        int n = in.nextInt();
        int m = in.nextInt();
        int k = in.nextInt();

        long l = -1;
        long r = n + 1;

        while (l + 1 < r) {
            long c = (l + r) / 2;
            if(n < c * k || canAchieve(n - c * k, k) >= m - c * k) {
                r = c;
            }
            else
                l = c;
        }

        //out.println(r);

        long c = r;

        answer = ((IntegerUtils.power(2, c + 1, MOD) - 2 + MOD) % MOD) * k % MOD;

        n -= k * c;
        m -= k * c;
        answer += m;
        answer %= MOD;

        out.println(answer);
    }

    private long canAchieve(long n, long k) {
        return n - n / k;
    }
}

class Reader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public Reader(BufferedReader reader) {
        this.reader = reader;
    }

    public Reader(InputStream stream) {
        this(new BufferedReader(new InputStreamReader(stream)));
    }

    public String nextString() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(readLine());
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(nextString());
    }

    private String readLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

class OutputWriter extends PrintWriter {

	public OutputWriter(OutputStream out) {
		super(out);
	}

	public OutputWriter(java.io.Writer writer){
		super(writer);
	}

	}

class IntegerUtils {

	public static long power(long base, long power, long mod) {
		long result = 1 % mod;
		base %= mod;
		while (power > 0) {
			if (power % 2 == 1) {
				result *= base;
				result %= mod;
			}
			base *= base;
			base %= mod;
			power >>= 1;
		}
		return result;
	}

	}
// Problem: 338_A. Quiz
// Time complexity: logn
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class A
{
	private static final long MOD = 1000000009L;
	public static void main(String [] args) throws IOException
	{
		Scanner in = new Scanner(System.in);
	    long n = in.nextInt();
		long m = in.nextInt();
		long k = in.nextInt();

		long w = n-m;

		long c = w*k;
		if(c >= n)
		{
			System.out.println(m);
			return;
		}

		long rem = n-c;
		long h = rem/k;

		long p = power(2, h+1);
		p -= 2;
		p += MOD;
		p %= MOD;
		p *= k;
		p %= MOD;

		long point = p + m - (h*k) % MOD;
		point += MOD;
		point %= MOD;

		System.out.println(point);



	}

	private static long power(int num, long power)
	{
		if(power == 0)
			return 1;
		long res = power(num, power/2);
		res = (res*res)%MOD;
		if(power % 2 != 0)
			res *= num;

		res%=MOD;

		return res;
	}
}
// Problem: 338_A. Quiz
// Time complexity: logn
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author ffao
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Parser in = new Parser(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskA {
    int mod = 1000000009;

    public void solve(int testNumber, Parser in, OutputWriter out) {
        int n = in.nextInt();
        int m = in.nextInt();
        int k = in.nextInt();

        int o = m;
        m -= n - (n/k);
        if (m < 0) m = 0;

        long temp = n/k;
        long ans;
        if (m == 0) ans = 0;
        else {
            ans = (MathUtils.modpow(2, m+1, mod) + mod - 2) % mod;
            ans = (ans * k) % mod;
        }

        out.println((ans + (o - m*k)) % mod);
    }
}

class Parser
{
    private BufferedReader din;
    private StringTokenizer tokenizer;

    public Parser(InputStream in)
    {
        din = new BufferedReader(new InputStreamReader(in));
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(din.readLine());
            } catch (Exception e) {
                throw new UnknownError();
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt()
    {
        return Integer.parseInt(next());
    }

    }

class OutputWriter extends PrintWriter {
    public OutputWriter(Writer out) {
        super(out);
    }

    public OutputWriter(OutputStream out) {
        super(out);
    }

    }

class MathUtils {

    public static long modpow(int b, int e, int m) {
        if (e == 0) return 1%m;
        else if (e == 1) return b%m;

        long temp = modpow(b, e/2, m);
        temp = (temp * temp) % m;
        if (e % 2 == 1) temp = (temp * b) % m;

        return temp;
    }

    }
// Problem: 338_A. Quiz
// Time complexity: logn


import java.util.Scanner;

public class A338 {

	public static void main (String args[]){
		
		Scanner in= new Scanner(System.in);
		long n = in.nextInt();
		long m=in.nextInt();
		long k=in.nextInt();
		
		long x = n-m;
		long y=n/k;
		if(x>=y)
			System.out.println(m);
		else
		{
			long t= y-x;
			long ans=0;
			ans+=k*(pow(t+1)-2);
			ans%=1000000009;
			ans+=m-t*k;
			ans%=1000000009;
			if(ans<0)
				ans+=1000000009;
			System.out.println(ans);
			
		}
			
		
		
	}
	
	public static long pow(long m ){
		
		if(m==1)
			return 2;
		long x = pow(m/2);
		
		x%=1000000009;
		x*=x;
		if(m%2!=0)
			x*=2;
		x%=1000000009;
		return (x);
		
	}
	
}
// Problem: 338_A. Quiz
// Time complexity: logn
import java.io.*;
import java.util.*;

public class A {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	void solve() throws IOException {
		int tot = nextInt();
		int ok = nextInt();
		
		int k = nextInt();
		
		int maxBad = tot / k;
		if (tot - maxBad >= ok) {
			out.println(ok);
			return;
		}
		
		
		int	dbl = ok + tot / k - tot;

		
		int dblPoints = pow(2, dbl + 1) - 2;
		while (dblPoints < 0)
			dblPoints += MOD;
		
		dblPoints = (int)((long)dblPoints * k % MOD);
		
		int rest = ok - dbl * k;
		
		int ans = dblPoints + rest;
		
		if (ans >= MOD)
			ans -= MOD;
		
		out.println(ans);
	}
	
	static int pow(int a, int b) {
		int ret = 1;
		while (b != 0) {
			if ((b & 1) == 1)
				ret = (int)((long)ret * a % MOD);
			a = (int)((long)a * a % MOD);
			b >>= 1;
		}
		return ret;
	}
	
	static final int MOD = 1000000009;

	A() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new A();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}
// Problem: 338_A. Quiz
// Time complexity: logn
import java.io.*;
import java.util.*;

public class A {
	long mod = (long)(1e+9+9);
	long pow(long a,long b) {
		long mul = a;
		long res = 1;
		while (b > 0) {
			if (b %2 == 1) {
				res = (res*mul)%mod;
			}
			mul = (mul*mul)%mod;
			b/=2;
		}
		return res;
	}

	void solve() throws IOException {
		long n = nextLong();
		long m = nextLong();
		long k = nextLong();
		long l = -1;
		long r = m / k;
		while (l < r - 1) {
			long mid = (l+r)/2;
			long leftOk = m - mid*k;
			long leftPos = n - mid*k;
			long cgroups = (leftOk + (k-2)) / (k-1);
			long positions = leftOk+cgroups-1;
			if (positions <= leftPos) {
				r = mid;
			} else {
				l = mid;
			}
		}
		long res = pow(2,r+1);
		res = (res - 2 + mod) %mod;
		res = (res*k) % mod;
		res = (res+m-r*k) %mod;
		out.println(res);
		
		
		
		
	}

	void run() throws IOException {
		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new A().run();
	}

	BufferedReader in;
	PrintWriter out;
	StringTokenizer st;

	String next() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			String temp = in.readLine();
			if (temp == null) {
				return null;
			}
			st = new StringTokenizer(temp);
		}
		return st.nextToken();
	}

	int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}

	long nextLong() throws IOException {
		return Long.parseLong(next());
	}

}
// Problem: 338_A. Quiz
// Time complexity: logn
import java.io.*;
import java.util.*;

public class ProblemA {
    InputReader in; PrintWriter out;

    long power(long a, long b, long mod) {
        long ret = 1;
        long mul = a;
        while (b > 0) {
            if (b % 2 == 1) {
                ret = (ret * mul % mod);                
            }
            mul = (mul * mul) % mod;
            b = b / 2;
        }
        return ret;
    }
    
    void solve() {
        long n = in.nextLong();
        long m = in.nextLong();
        long k = in.nextLong();
        long mod = 1000000009;
        long x = m - (n - n / k);
        if (x <= 0) {
            out.println(m);
        }
        else {
            long score = 1;
            score = power(2, x + 1, mod);
            score = (score + mod - 2) % mod;
//          out.println(score);
            long ans = ((score * k) + m - x * k + mod) % mod;
            out.println(ans);
        }
    }
    
    ProblemA(){
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;
        try {
            if (oj) {
                in = new InputReader(System.in);
                out = new PrintWriter(System.out);
            }
            else {
                Writer w = new FileWriter("output.txt");
                in = new InputReader(new FileReader("input.txt"));
                out = new PrintWriter(w);
            }
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
        solve();
        out.close();
    }
    public static void main(String[] args){
        new ProblemA();
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }
    
    public InputReader(FileReader fr) {
        reader = new BufferedReader(fr);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }
    public int nextInt() {
        return Integer.parseInt(next());
    }
    public long nextLong() {
        return Long.parseLong(next());
    }
    public double nextDouble() {
        return Double.parseDouble(next());
    }

}
// Problem: 338_A. Quiz
// Time complexity: logn
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class C
{
	String line;
	StringTokenizer inputParser;
	BufferedReader is;
	FileInputStream fstream;
	DataInputStream in;
	String FInput="";
	
	
	void openInput(String file)
	{

		if(file==null)is = new BufferedReader(new InputStreamReader(System.in));//stdin
		else
		{
			try{
		
				
			fstream = new FileInputStream(file);
			in = new DataInputStream(fstream);
			is = new BufferedReader(new InputStreamReader(in));
			}catch(Exception e)
			{
				System.err.println(e);
			}
		}

	}
	
	void readNextLine()
	{
		try {
			line = is.readLine();
			inputParser = new StringTokenizer(line, " ");
			//System.err.println("Input: " + line);
		} catch (IOException e) {
			System.err.println("Unexpected IO ERROR: " + e);
		}	
		
	}
	
	int NextInt()
	{
		String n = inputParser.nextToken();
		int val = Integer.parseInt(n);
		
		//System.out.println("I read this number: " + val);
		return val;
	}
	
	String NextString()
	{
		String n = inputParser.nextToken();
		return n;
	}
	
	void closeInput()
	{
		try {
			is.close();
		} catch (IOException e) {
			System.err.println("Unexpected IO ERROR: " + e);
		}
			
	}
	
	public void readFInput()
	{
		for(;;)
		{
			try
			{
				readNextLine();
				FInput+=line+" ";
			}
			catch(Exception e)
			{
				break;
			}
		}
		inputParser = new StringTokenizer(FInput, " ");
	}
	
	long NextLong()
    {
            String n = inputParser.nextToken();
            
            long val = Long.parseLong(n);
            
            return val;
    }
	
	public static void main(String [] argv)
	{
		//String filePath="input.txt";
        String filePath=null;
        if(argv.length>0)filePath=argv[0];
		new C(filePath);
	}
	final int MOD = 1000000009;
	public C(String inputFile)
	{
		openInput(inputFile);
		StringBuilder sb = new StringBuilder();
		readNextLine();
    	int N=NextInt(), M=NextInt(), K=NextInt();
    	
    	if((N/K)<=(N-M))
    	{
    		sb.append(M);
    	}
    	else
    	{
    		int x=(N/K)-(N-M);
    		long ret=(pow(2, x) -1 );
    		
    		
    		ret *=K;
    		ret%=MOD;
    		ret *= 2;
    		ret%=MOD;
    		/*
    		ret+=pow(K, x+1);
    		ret%=MOD;*/
    		ret+=(M-x*K);
    		ret%=MOD;
    		sb.append(ret+"\n");
    		
    		
    	}
		
    	System.out.println(sb);
		closeInput();		
	}

	long pow(long b, long exponent)
	{
	    long ret = 1;
	    while(exponent > 0)
	    {
	        if (exponent%2 == 1)
	           ret = (ret * b) % MOD;
	        exponent = exponent >> 1;
	        b = (b * b) % MOD;
	    }
	    return ret;
	}

	
}
// Problem: 338_A. Quiz
// Time complexity: logn
import java.io.*;
import java.util.*;

public class Main implements Runnable {

	public void _main() throws IOException {
		long n = nextLong();
		long m = nextLong();
		long k = nextLong();
		long numBlocks = Math.min(Math.min(n / k, n - m), m / (k - 1));
		n -= numBlocks * k;
		m -= numBlocks * (k - 1);
		long numFullBlocks = m / k;
		long rem = m % k;
		long res = 0;
		res = (res + ((p2(numFullBlocks + 1) + MOD - 2) % MOD) * k) % MOD;
		res = (res + rem) % MOD;
		res = (res + numBlocks * (k - 1)) % MOD;
		out.println(res);
	}

	final int MOD = 1000000009;
	private long p2(long s) {
		long res = 1;
		long x = 2;
		while (s > 0) {
			if (s % 2 == 1) {
				res = res * x % MOD;
			}
			x = x * x % MOD;
			s /= 2;
		}
		return res;
	}

	private BufferedReader in;
	private PrintWriter out;
	private StringTokenizer st;

	private String next() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			String rl = in.readLine();
			if (rl == null)
				return null;
			st = new StringTokenizer(rl);
		}
		return st.nextToken();
	}

	private int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	private long nextLong() throws IOException {
		return Long.parseLong(next());
	}

	private double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}

	public static void main(String[] args) {
		Locale.setDefault(Locale.UK);
		new Thread(new Main()).start();
	}

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			//in = new BufferedReader(new FileReader("a.in"));
			//out = new PrintWriter(new FileWriter("a.out"));

			_main();

			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(202);
		}
	}

}