HDU 1420 (prepared for new acmer) (China residue theorem) (power reduction method)

Source: Internet
Author: User

Prepared for new acmertime limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others) Total submission (s): 6516 accepted submission (s): 2450

The Problem description training lasted for nearly two weeks. During this period, we focused mainly on recoverability training. I have been paying close attention to the training situation. So far, I have been quite satisfied with your performance, first, most of the team members are very enthusiastic about training, and secondly, they observe the discipline of training. Finally, the old team members also played a very leading role, I would like to thank xhd, the training team leader who provided questions and test data for this DP special exercise competition.

I'm particularly pleased that a group of new players trained by the training team have performed very well and made remarkable progress. Especially, the training attitude has greatly exceeded my expectation. I dare say, if you can stick to it like this, there is no way ahead!

Considering that new players have not been trained in the system, I will add a simple question here:
Returns the result of a ^ B mod C given three integers A, B, and C (a, B, c <= 1000000.

I hope all of you will be able to see the joy of AC in the competition, absolutely tailored, and enjoy a high level of treatment...

Input data first contains a positive integer N, indicating the number of test instances, followed by N rows of data, each line contains three positive integers A, B, C.

Output outputs the computation results for each test instance. The output of each instance occupies one row.

Sample Input
32 3 43 3 54 4 6

Sample output
024

Authorlcy
Sourceacm summer training team exercise session (2)
Question Analysis: To solve this question, we must first know this formula (A * A) % C = (a % C) * (a % C) % C ...... Formula 1.

Then we use the power-down method. Let's take an example to illustrate,

3 ^ 8 = 3 ^ 4*3 ^ 4 = (3 ^ 2*3 ^ 2) * (3 ^ 2*3 ^ 2) = (3*3) * (3*3) * (3*3) * (3*3). If 3 ^ 8% 5 is required, obtain 3% 5 first,
According to Formula 1, 3 ^ 2%, 3 ^ 4%, 3 ^ 8%, 3 ^, 5 can be obtained in sequence. This is a process in which the power is reduced by 2.

It should be noted that the power of a certain power division of 2 may become an odd number. At this time, we need to extract one power first and then reduce the power,
For example, 3 ^ 10 = 3 ^ 5*3 ^ 5 (Power: 5, odd) = (3 ^ 2*3 ^ 2*3) * (3 ^ 2*3 ^ 2*3) = ......
From: Click to open the link
TIPS: I have benefited a lot and learned something! The Code is as follows:
#include<stdio.h> int main(){int i,n,a,b,c;scanf("%d",&n);while(n--){__int64 temp,sum;scanf("%d%d%d",&a,&b,&c);sum=a%c;temp=1;while(b>1)//因为起初的时候,已经是a%c了所以已经是一次方了 {if(b&1)//奇数的话,单独拉出来 {temp*=sum;//temp用来存储奇数情况下的所有的乘积 temp%=c;b--;//单独拉出来,次数减一 }else{sum*=sum;//降幂法 sum%=c;b/=2;//因为是变化后乘方,所以次方数减半 }}printf("%I64d\n",sum*temp%c);}return 0;}


HDU 1420 (prepared for new acmer) (China residue theorem) (power reduction method)

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.