Editorial for K-DIGITS


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của happyboy99x

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String argv[]) {
        Scanner inp = new Scanner(System.in);
        int n = inp.nextInt();
        BigInteger k = inp.nextBigInteger().subtract(BigInteger.ONE);
        if(n == 1) System.out.println(k.add(BigInteger.ONE));
        else {
            BigInteger f[][] = new BigInteger[n][2];
            f[0][1] = k;
            f[0][0] = BigInteger.ZERO;
            for(int i = 1; i < n; ++i) {
                f[i][0] = f[i-1][1];
                f[i][1] = k.multiply(f[i-1][0].add(f[i-1][1]));
            }
            System.out.println(f[n-1][0].add(f[n-1][1]));
        }
    }
}

Code mẫu của RR

import java.io.*;
import java.math.*;
import java.util.*;

public class Main
{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), k = sc.nextInt();

        if (n == 1) {
            System.out.println(k);
            return ;
        }

        BigInteger[][] f = new BigInteger[n+2][2];
        f[1][0] = BigInteger.ZERO;
        f[1][1] = BigInteger.valueOf(k-1);

        for(int i = 2; i <= n; ++i) {
            f[i][0] = f[i-1][1];
            f[i][1] = (f[i-1][0].add(f[i-1][1])).multiply(BigInteger.valueOf(k-1));
        }
        System.out.println(f[n][0].add(f[n][1]));
    }
}

Code mẫu của hieult

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {
    public static void main(String args[]) throws Exception{
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), k = sc.nextInt();
        BigInteger[][] A = new BigInteger[10005][2];

        if(n == 1){
            System.out.println(k);
        }

        else{
            A[1][0] = BigInteger.valueOf(k - 1);
            A[1][1] = BigInteger.ZERO;

            for(int i = 2; i <= n; i++){
                A[i][0] = (A[i - 1][0].add(A[i - 1][1])).multiply(BigInteger.valueOf(k - 1));
                A[i][1] = A[i - 1][0];
            }

            System.out.println(A[n][0].add(A[n][1]));
        }

    }
}

Code mẫu của skyvn97

#include<stdio.h>
#include<vector>
using namespace std;
typedef vector<int> vi;
struct bignum
{
       vi d;
       bignum()
       {
        d.clear();
       }
       bignum(int x)
       {
        bignum();
        if (x==0) d.push_back(0);
        while (x>0)
              {
               d.push_back(x%10);
               x=x/10;
              }           
       }      
       bignum(const bignum &x)
       {
        d=x.d;
       } 
       bignum operator + (const bignum &x)
       {
        bignum res=bignum();
        int n=max(d.size(),x.d.size());        
        int i,s,c,a,b;
        s=0;
        c=0;
        for (i=1;i<=n;i=i+1)
            {
             if (i>d.size()) a=0; else a=d[i-1];
             if (i>x.d.size()) b=0; else b=x.d[i-1];
             s=a+b+c;
             if (s>9) c=1; else c=0;
             res.d.push_back(s%10);
            }
        if (c>0) res.d.push_back(1);           
        while (res.d[res.d.size()-1]==0) res.d.pop_back();
        return (res);
       }
       bignum operator * (int x)
       {
        if (x==0) return (bignum(0));
        if ((d.size()==1) && (d[0]==0)) return (bignum(0));
        bignum res=bignum();
        int i,s,c;
        s=0;
        c=0;
        for (i=1;i<=d.size();i=i+1)
            {
             s=d[i-1]*x+c;
             c=s/10;
             res.d.push_back(s%10);
            }
        while (c>0)
              {
               res.d.push_back(c%10);
               c=c/10;
              }
        while (res.d[res.d.size()-1]==0) res.d.pop_back();
        return (res);
       }
       void print(void)
       {
            int i;
            for (i=d.size();i>=1;i=i-1) printf("%d",d[i-1]);
       }
};
bignum f[3];
bignum g[3];
bignum r;
int n,i,k;
int main(void)
{      

    scanf("%d",&n);
    scanf("%d",&k);
    if (n==1)
       {
        printf("%d",k);
        return 0;
       }    
    f[1]=bignum(k-1);
    g[1]=bignum(0);
    for (i=2;i<=n;i=i+1)
        {
         f[i%2]=(f[(i-1)%2]+g[(i-1)%2])*(k-1);
         g[i%2]=f[(i-1)%2];
        }
    r=f[n%2]+g[n%2];
    r.print();
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.