NYOJ 420 P-Squared summation (fast Power + congruence theorem)

Source: Internet
Author: User

Title Description:

http://acm.nyist.net/JudgeOnline/problem.php?pid=420

A very simple question, beg 1^p+2^p+3^p+......+n^p and.
Input
The first line of a single number t represents the number of test data groups. Next there will be T-line numbers, each line consisting of two digital n,p,
Enter the guaranteed 0<n<=1000,0<=p<=1000.
Output
Output 1^p+2^p+3^p+......+n^p The result of 10003, each result is a single row.
Sample input
210 110 2
Sample output
55385

Topic Analysis:

The problem of fast Power + congruence theorem, although has been written many times the fast power but still do not remember, each time is to see the template, this time must remember it.


AC Code:

/** * Quick Power get touch + +/#include <iostream> #include <cstdio> #include <map> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <vector> #include <stack># include<cstdlib> #include <cctype> #include <cstring> #include <cmath>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<<t<<endl;            Res= (res%10003+t%10003)%10003;        }        cout<<res<<endl;    } return 0;}        




NYOJ 420 P-Squared summation (fast Power + congruence theorem)

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.