"Simulation" Rabbit Farm

Source: Internet
Author: User
Tags time 0

Free farming CH Round #54-streaming #5 (Noip simulation Day1)Describe

(If you want to better understand the subject, please read NOI2011 first try "Rabbit Farm" a problem)
When she was worried about how to make more money in recent years, she heard the children next door talking about the problem of child-free reproduction. (Note: Child-free is a simple single-celled organism)
The problem is this: time 0 has 2 newborn children. Every moment, each child will be divided into 2 free children. Q Time n How many free children?
Smart you may have found that the moment N of the number of n+1 is exactly the power of the first 2. Meng eggs do not understand what is called power, but she also found the law: the moment n+1 the number of free children equal to the time n the number of free children twice times. The number of free children in the first few moments (starting from 0) is:
2 4 8 16 32 64 128 256 512 ...
Meng eggs found more to the back of the increase in the number of children, expect to raise the son must be able to make a lot of money, so the egg at the moment 0 bought 2 free children began to cultivate.
Every day, the eggs are to give the children to provide nutrition. The medium of the child-free is very special, each K-free child occupies a medium, the last remaining less k occupies only one medium. Because the child is particularly afraid of loneliness, if a medium only 1 free of children, the child will soon die.
However, the number of free children at each moment can still be calculated. For example, when k=7, the number of free children from the first few moments (starting at 0) is:
2 4 7 14 28 56 112 224 448 ...
Given n, you can help the egg to calculate the moment n how many children do she have? Because the answer can be very large, you just need to tell the egg-n-free number to the remainder of P.

Input format

The input has only one row and contains three integers n k p.

Output format

The output has only one row, which is an integer that represents the remainder of the time N's free-of-number pairs p.

Sample input
6 7 10086
Sample output
112
Data scope and conventions

For 30% of data, n≤1,000,000.
Another 30% of the data, K is the positive integer multiples of p.
for 100% of data, n≤1,000,000,000,2≤k≤1,000,000,1≤p≤1,000,000.

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <string > #include <cmath> #include <algorithm> #include <queue> #include <vector> #include <set >using namespace std; #define INF 1<<30#define LL long longll n,k,p; ll Pow (ll x) {      ll ans=1,y=2;      while (x)      {          if (x&1) ans=ans*y%p;          Y= (y*y)%p;          x=x>>1;      }      return ans%p;} int main () {      scanf ("%i64d%i64d%i64d", &n,&k,&p);      LL ans=2,tem=2;      if (k%2==0)      {            printf ("%i64d\n", 2*pow (n));            return 0;      }      for (int i=1;i<=n;i++)      {         tem= (tem*2)%k;         if (tem%k==1)         {            printf ("%i64d\n", (ans*2-1) *pow (n-i)%p);            return 0;         }         Ans= (ans*2)%p;      }      printf ("%i64d\n", ans);      return 0;}

  

"Simulation" Rabbit Farm

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.