NYOJ 420 p power summation (Fast Power + same remainder theorem)

Source: Internet
Author: User

NYOJ 420 p power summation (Fast Power + same remainder theorem)

Description:

 

A very simple question: 1 ^ p + 2 ^ p + 3 ^ p + ...... + The sum of n ^ p. Enter a separate number t in the first line to indicate the number of test data groups. Next, there will be t rows of numbers, each line includes two numbers n, p,
Input 0 Output 1 ^ p + 2 ^ p + 3 ^ p + ...... + N ^ p returns the remainder of 10003, and each result occupies a single row. Sample Input
210 110 2
Sample output
55385

Question Analysis:

The question of the Fast Power + conremainder theorem is that although the fast power has been written for many times, I still don't remember it. Every time I read the template, I must remember it this time.

 

AC code:

 

/*** Quick power touch acquisition + same remainder */# include
  
   
# Include
   
    
# Include
    # Include
     
      
# Include
      
       
# Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           
             # Include
            
              # Include
             
               Using namespace std; int mod (int a, int B, int n) {int t = 1; if (B = 0) return 1; if (B = 1) return a % n; t = mod (a, B> 1, n); t = t * t % n; if (B & 1) {// B is an odd t = t * a % n;} return t;} int main () {int t; cin> t; while (t --) {int p, n; cin >>n> p; int res = 0; for (int I = 1; I <= n; I ++) {int t = mod (I, p, 10003); // cout <
              
               

 

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.