[Problem] uva11029 leading and trailing

Source: Internet
Author: User

Link: http://vjudge.net/problem/viewProblem.action? Id = 19597

Description: calculates the first three digits and the last three digits of N ^ K.

Idea: the question should solve two problems. The last three digits can be used to evaluate the modulo of the higher power. The following describes how to evaluate the first three digits.

N ^ k = (10 ^ lg n) ^ K = 10 ^ (K * lg n)

For ease of description, make X = n ^ K. The integer part of lg x indicates the number of digits of X. If the integer is Zs and the fractional part is XS, x = (10 ^ ZS) * (10 ^ XS ). (10 ^ ZS) in the form of 100 ......, Like the number of digits in X, (10 ^ XS) is at 100 ...... Then, modify the number on each digit to convert it into X. So (10 ^ XS) * 100 is the first three digits of X (if you don't think clearly, you can draw a picture on the paper ).

 

The following is my implementation.

 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 #define MOD 1000 6 int T,N,K; 7 int Trail; 8 double Lead; 9 inline void Get_int(int &Ret)10 {11     char ch;12     bool flag=false;13     for(;ch=getchar(),ch<‘0‘||ch>‘9‘;)14         if(ch==‘-‘)15             flag=true;16     for(Ret=ch-‘0‘;ch=getchar(),ch>=‘0‘&&ch<=‘9‘;Ret=Ret*10+ch-‘0‘);17     flag&&(Ret=-Ret);18 }19 int My_Pow(int n,int k)20 {21     if(k==1)22         return n%MOD;23     int p=My_Pow(n,k/2);24     if(k%2)25         return (p*p*(n%MOD))%MOD;26     return (p*p)%MOD;27 }28 int main()29 {30     Get_int(T);31     while(T--)32     {33         Get_int(N);Get_int(K);34         Lead=(double)K*log10(N);35         Lead=Lead-(int)Lead;36         Lead=pow((double)10,2+Lead);37         Trail=My_Pow(N,K);38         printf("%d...%03d\n",(int)Lead,Trail);39     }40     return 0;41 }
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.