Thursday, January 19, 2017

Solution of UVa-11029 Leading and Trailing


#include<bits/stdc++.h>
#define ll long long

using namespace std;

ll n, k, t_case;

ll bigmod(ll b, ll p, ll m){

    if(p==0)return 1;

    ll xx=bigmod(b, p/2, 1000);
    xx=(xx*xx)%1000;

    if(p%2==1)xx=(xx*b)%1000;

    return xx;
}

int main(){

    cin>>t_case;

    while(t_case){

        cin>>n>>k;

        /* executing first 3 digits */

        double x=k*(log10(n));

        x=x-(int)x; // taking fraction value only
        
        double ans=pow(10, x);

        ans=ans*100;

        cout<<(int)ans<<"...";

        /* executing last 3 digits */

        printf("%03d\n", bigmod(n, k, 1000));

        t_case--;

    }

    return 0;
}

5 comments:

  1. https://epmahfuz.blogspot.com/2017/01/solution-of-uva-11029-leading-and.html

    ReplyDelete
  2. Can you please explain the idea behind it ,thanks in advance

    ReplyDelete
    Replies
    1. p=x^k
      log10(p)=log10(x^k)
      log10=klog10(x)// let log10(p)=124.234232
      now 10^RHS givs me x^k
      so 10^124.234232=10^124*10^.234232
      10^124 will just move decimal point only
      so 10^.234232 gives me solution
      now x=10^.234332=1.71526805726
      so we will multiply this x with 100 because 10^(any float value less than 1 and greater than 0) gives me answer from 1.000001... to 9.9999999...
      therefor x*100=171.526805726
      now int(x) gives me 171 as answer

      Delete
  3. https://www.geeksforgeeks.org/given-number-n-find-first-k-digits-nn/

    ReplyDelete
  4. whats wrong with the following java solution ?

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

    public class Main {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while (t-- > 0) {
    long n = sc.nextLong();
    long k = sc.nextLong();
    double power = (k * Math.log10(n));
    int firstDigits = (int) (Math.pow(10, power - Math.floor(power)) * 100);
    int lastdigits = Integer.parseInt(String.valueOf(new BigInteger(String.valueOf(n)).modPow(new BigInteger(String.valueOf(k)), BigInteger.valueOf(1000))));
    System.out.println(firstDigits + "..." + lastdigits);
    }
    }
    }

    ReplyDelete