Virus Splitting (division)

Source: Internet
Author: User

Title: Virus Splitting

"Problem description"


a The school lab has developed a very powerful virus. Because the virus was so hard to make artificially, the experts made only one such virus at the beginning.

This virus is implanted with a special microchip that can have some special programmable performance. One of the most important features is that experts can set their own ability to split the virus k x A virus, the next split cycle will have kx an identical virus. You, as the data analyst for the lab, need to count the divisions to the n before the cycle (note that not the final number of cells ...). lz 0.0 ) . There is always only one virus at the beginning, and this situation counts as the first cycle. Since the answer may be large, the experts only need you to tell them about the given p

"Input Format"


A row of three integers, followed by K, N, P.

"Output Format"


One line of an integer, your answer (modulo P ).

"Input Sample"


"Sample 1"
 5 3 7

"Sample 2"
 2 6 23

"Output Example"


"Sample 1"
 6

"Sample 2"
 8

"Sample Interpretation"


Example One explanation: the first cycle has 1 viruses, resulting in a split. The second cycle has 1*5=5 virus, the five viruses will split. So a total of 1+5 equals 6 splits before the third cycle . The answer is 6 mod 7 = 6.

"Data Range"


1 < N < 10^18
1 < K, P < 2^31

Source


The eight-medium proposition.

Topic Analysis:

For this kind of topic, we might as well take the first example analysis on the draft paper.

We now assume that our virus can split 3 times at a time (sloth!). ), and we want to calculate the number of splits within 4 weeks (note the number of splits, not the final number of splits ...). LZ That's the wrong thing to do.

it is easy to deduce from our example that the first N the number of cells that divide during the week is 1+n+n*n+n^3+n^4+......+n^ (n-2)

So here you have finished the majority of the problem, the rest is the analysis of the above-said

method 1: Brute Force solution

Brute force enumeration, honestly calculate the power of each item, and then Add. Of course, the simplest method can only be achieved by a score of. (please look at the data range carefully)

method 2: divide and conquer the solution!

The first thing we want to think about is two points ( LZ too little to see ... )

We can divide into two plates: the power of the K and the sum of k^0 = k^ (n-2) and (exponentiation)

Please go directly to the code to view the detailed explanation

Skills:

Someone asked, since the brute force solution to get a full mark, then LZ will it in the list of what is the point? Very simple, it is simple and good to write, when we can not think of other methods, we may wish to write it thick to guarantee the score. Well of course I absolutely did not laugh at XX IQ meaning, then for the thought of the division of the method of the students what is the use of it, then we will mention a competition skills:

For a problem we can always think of 2 or more than 2 ways, then in addition to the method of unexpected, the other one or several methods can be compared with the full score. As we all know, test methods can easily be thought out, but more difficult is how to get the code of the method to achieve the correct. So for this question we are wrong often not ultra, but in the code error. On the contrary, although the violent method will time out, but the code error is basically not there (PS: By their own face ... And then we can use the Brute force program to test our full-score program to find errors.

Well, with so many passages of water, let's take a look at how easier it is to achieve the PAT.

First of all, our program input file name is called:virus.in output file name:virus.out

Then we have the same name for the program input:virus.in But the output file name is:virus_dp.out

At the same time we need a data generator (reference code) Virus_data.exe(available in C + + compilation)

and a small program to compare the output data (the system comes with:FC can be directly searched, usually in the C disk)

Then we create a new bat file and edit it in it:

@echo off to clear miscellaneous instructions, you can try not to add the consequences of this sentence (please do not try)

: Loop loops with back Onto loop

Virus_data.exe running Data generators

Virus.exe running out of the program

Virus_dp.exe running the PAT program

FC Virus.out virus_dp.out compare output data for both

If not errorlevel 1 onto Loop compares the next set of data if there is no error

Pause If there is a mistake

Onto Loop compares the next set of data

Code Place:

1. Full out Code

#include <iostream>
#include <fstream>
#include<cstdio>#definell Long Longusing namespacestd;ll k,n,p;ll Qkpow (ll a,ll b) {//Two-point exponentiation if(b==0)return 1;//give the boundary: when the calculation a^0 returns answer 1ll m=b/2;//dichotomy Indexll cmt= (Qkpow (a,m))%p;//first find out the value of A^mll s= (CMT*CMT)%p;//through a^n=a^m*a^m to find out A^n if(b&1) s= (s*a)%p;//If n is odd, it should be multiplied by an a. returnS}ll Solve (ll a,ll b) {//Two-point exponentiation and A^1+a^2+....+a^b if(b==1)returnA//Give the boundary: return answer a when calculating a^1ll m=b/2; ll Cmt=solve (a,m);//Find out a^1+a^2+....+a^m=cmpll T=qkpow (a,m); ll s= (CMT*T+CMT)%p;//through A^1+A^2+...A^N=CMP*A^M+CMP if(b&1) s= (S+qkpow (b))%p;//also special treatment of odd B returnS}intMain () {
Freopen ("Virus.in", "R", stdin);
Freopen ("Virus.out", "w", stdout); ll na; CIN>>k>>n>>p; ll ans= (Solve (k,n-2)+1)%p; cout<<ans; return 0;}

2. Data Generator

#include <cstdio>#include<cstdlib>#include<ctime>using namespacestd;intMain () {Freopen ("virus.in","W", stdout);   Srand (Time (NULL)); intn,k,p; N=rand () +2; K=rand () +2; P=rand () +2; printf ("%d%d%d\n", k,n,p); return 0; }

Virus Splitting (division)

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.