UV-11029-Leading and Trailing (Rapid power + formula deformation)

Source: Internet
Author: User

UV-11029-Leading and Trailing (Rapid power + formula deformation)

 

 

Idea: the last three digits can be directly used for power modulo, and the first three digits can be used in two ways: double and public. For details, refer to the code.

 

Note that the last three digits must be 0, I .e. % 03d.

 

AC code ①:

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        # Include
         
           # Include
          
            # Include
           
             # Define LL long # define INF 1000000000 using namespace std; # define MOD 1000int T; int n, k; int kmod (int x, int n) {// fast power int ret = 1; while (n) {if (n & 1) ret = (ret * x) % MOD; x = (x * x) % MOD; n >>= 1;} return ret;} double kkmod (double x, int n) {// calculate the first three double ret = 1 using double; while (n) {if (n & 1) ret = ret * x; while (ret> = INF) ret/= INF; x = x * x; while (x> = INF) x/= INF; n >>= 1;} return ret;} int main () {scanf (% d, & T); while (T --) {scanf (% d, & n, & k); int ttt = n % 1000; ttt = kmod (ttt, k); double lll = kkmod (double) n, k); lll * = 1000; // maybe lll is less than 1000, maybe less than three while (lll> = 1000) {lll/= 10 ;} /* char str [1234]; sprintf (str, % lf, 1000 * lll); str [3] = ''; * /// you can also output the first three digits // printf (% lf, lll); printf (% d... % 03d, (int) lll, ttt); // remember to use % 03d for the last three digits. Output in strict accordance with the format} return 0 ;}
           
          
         
       
      
     
    
   
  
 


 

 

 

AC code ②:

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        # Include
         
           # Include
          
            # Include
           
             # Define LL long # define INF 0x7fffffffusing namespace std; int T; int n, k; int kmod (int x, int n) {int ret = 1; while (n) {if (n & 1) ret = (ret * x) % 1000; x = x * x % 1000; n >>= 1;} return ret;} int main () {scanf (% d, & T); while (T --) {scanf (% d, & n, & k); int lll, ttt; lll = kmod (n % 1000, k); ttt = (int) pow (10, 2 + fmod (k * log10 (n), 1 )); // calculate the first three digits of printf (% d... % 03d, ttt, lll);} return 0 ;}
           
          
         
       
      
     
    
   
  
 


 

 

 

 

 

 

 

 

 

 

 

 

 

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.