HDU 4990 Reading comprehension(BestCoder Round #8)

Problem Description:

Read the program below carefully then answer the question.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>

const int MAX=100000*2;
const int INF=1e9;

int main()
{
  int n,m,ans,i;
  while(scanf("%d%d",&n,&m)!=EOF)
  {
    ans=0;
    for(i=1;i<=n;i++)
    {
      if(i&1)ans=(ans*2+1)%m;
      else ans=ans*2%m;
    }
    printf("%d\n",ans);
  }
  return 0;
}

Input:

Multi test cases,each line will contain two integers n and m. Process to end of file.
[Technical Specification]
1<=n, m <= 1000000000

Output:

For each case,output an integer,represents the output of above program.

Sample Input:

1 10

3 100

Sample Output:

1

5

通过观察发现当n为偶数时,第n项为a = (2^(n+1)-2)/3;当n为奇数时,第n项为a = (2^(n+1)-1)/3,由于要对m取余,那么结果ans = a/3%m,这里要用到数模计算公式:a/b%m = a%(b*m)/b,所以ans = a%3m/3。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;

const int N=1e4+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;

typedef long long LL;

LL Solve(LL a, LL b, LL m) ///快速幂
{
    LL t, sum;

    t = a % m;
    sum = 1;

    while (b)
    {
       if(b&1) sum = (sum*t)%m;

       t = (t*t)%m;
       b /= 2;
    }

    return sum;
}

int main ()
{
    LL n, m, ans;

    while (scanf("%lld %lld", &n, &m) != EOF)
    {
        ans = Solve(2, n+1, 3*m);

        if (n % 2 == 0) ans = (ans-2)/3;
        else ans = (ans-1)/3;

        printf("%lld\n", ans);
    }

    return 0;
}
时间: 2024-08-27 22:25:50

HDU 4990 Reading comprehension(BestCoder Round #8)的相关文章

hdu 4990 Reading comprehension (矩阵快速幂)

题意:读程序,找规律 思路:我们把程序输出发现序列为1,2,5,10,21,42,85,170,递推式f(n)=2*f(n-2)+f(n-1)+1 代码: #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #define ll long long using namespace std; const int N=3,M=3,P=3; ll mod; stru

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

hdu 4990 Reading comprehension(等比数列法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 思路:曾经有一个矩阵乘法的做法请戳这儿.. . . 開始我们把数都不模... 能够得到一个规律 n:1        ans:1      4^0                          n:2     ans:2         2*(4^0) 2                 5      4^0+4^1                        4           

HDU 4990 Reading comprehension (找规律+矩阵快速幂)

题目链接:HDU 4990 Reading comprehension 题目给的一个程序其实就是一个公式:当N=1时 f[n]=1,当n>1时,n为奇数f[n]=2*f[n-1]+1,n为偶数f[n]=2*f[n-1]. 先不取模,计算前十个找规律.得到一个递推公式:f[n]=2*f[n-2]+f[n-1]+1 然后快速幂解决之. 给出一个神奇的网站(找数列通项):http://oeis.org/ AC代码: #include<stdio.h> #include<string.h&

HDU 4932 Miaomiao&#39;s Geometry(BestCoder Round #4)

Problem Description: There are N point on X-axis . Miaomiao would like to cover them ALL by using segments with same length. There are 2 limits: 1.A point is convered if there is a segments T , the point is the left end or the right end of T.2.The le

HDU 5056 Boring count(BestCoder Round #11 (Div. 2))

Problem Description: You are given a string S consisting of lowercase letters, and your task is counting the number of substring that the number of each lowercase letter in the substring is no more than K. Input: In the first line there is an integer

HDU 5067 Harry And Dig Machine(BestCoder Round #14)

Problem Description: As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as

HDU-5086-Revenge of Segment Tree (BestCoder Round #16)

Revenge of Segment Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 205    Accepted Submission(s): 83 Problem Description In computer science, a segment tree is a tree data structure for st

HDU-5108-Alexandra and Prime Numbers (BestCoder Round #19)

Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 319    Accepted Submission(s): 120 Problem Description Alexandra has a little brother. He is new to programming. One