The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart.

It is known that y=(5+2 *sqrt(6))^(1+2^x)

For a given integer x(0≤x<2^32) and a given prime number M(M≤46337), print [y]%M. ([y]means the integer part of y)

Input Format

An integer T(1<T≤1000), indicating there are T test cases.

Following are T lines, each containing two integers x and M, as introduced above.

Output Format

The output contains exactly T lines.

Each line contains an integer representing [y]%M.

样例输入

7
0 46337
1 46337
3 46337
1 46337
21 46337
321 46337
4321 46337

样例输出

Case #1: 97
Case #2: 969
Case #3: 16537
Case #4: 969
Case #5: 40453
Case #6: 10211
Case #7: 17947

题目来源

ACM-ICPC 2015 Shenyang Preliminary Contest

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100005//一开始是10005,一直超时/
#define mod 1000000007
#define gep(i,a,b) for(int i=a;i<=b;i++)
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
/*
a(n)=(5+sqrt(6))^n,b(n)=(5-sqrt(6))^n
c(n)=a(n)+b(n)//是个整数
a(n)-(1-b(n))=a(n)+b(n)-1=c(n)-1为a(n)向下取整的结果。
c(n)*((5+sqrt(6))+(5-qrt(6)))
因此10*c(n)=c(n+1)+c(n-1)
c(n+1)=10*c(n)-c(n-1)
*/
int t;
ll x,m;
ll c[N];
ll len;
ll solve(){
c[]=%m;
c[]=%m;
//cout<<m<<endl;
for(int i=;;i++)
{
c[i]=(*c[i-]-c[i-]+m)%m;
//cout<<c[i]<<endl;
if(c[i-]==c[]&&c[i]==c[]){
//cout<<len<<endl;
return len=i-;
}
}
}
ll poww(ll a,ll b,ll p){
ll ans=%p;
a%=p;
while(b){
if(b&) ans=(ans*a)%p;
b>>=;
a=(a*a)%p;
}
return ans%p;
}
int main()
{
scanf("%d",&t);
gep(i,,t){
scanf("%lld%lld",&x,&m);//1+2^x(x很大)一定有循环节。
//cout<<x<<" "<<m<<endl;
solve();//因为%m的m不大,因此每次都去找循环节。
ll id=poww(,x,len);
id=(id+)%len;
printf("Case #%d: %lld\n",i,((c[id]-)%m+m)%m);
}
return ;
}

ACM-ICPC 2015 Shenyang Preliminary Contest B. Best Solver的更多相关文章

  1. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering

    Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...

  2. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time

    Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...

  3. ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków

    ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...

  4. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 I. Illegal or Not?

    I. Illegal or Not? time limit per test 1 second memory limit per test 512 megabytes input standard i ...

  5. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 K. King’s Rout

    K. King's Rout time limit per test 4 seconds memory limit per test 512 megabytes input standard inpu ...

  6. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 H. Hashing

    H. Hashing time limit per test 1 second memory limit per test 512 megabytes input standard input out ...

  7. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 C. Colder-Hotter

    C. Colder-Hotter time limit per test 1 second memory limit per test 512 megabytes input standard inp ...

  8. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 A. Anagrams

    A. Anagrams time limit per test 1 second memory limit per test 512 megabytes input standard input ou ...

  9. Samara SAU ACM ICPC 2013-2014 Quarterfinal Qualification Contest

    A: 简单题,因为题目中说了不会有数据相同: #include<cstdio> #include<algorithm> #define maxn 200005 using na ...

随机推荐

  1. 1137 - Sin your life sin公式 + 枚举

    http://www.ifrog.cc/acm/problem/1137 和差化积公式, 变成2 * sin((x + y) / 2) * cos((x - y) / 2) + sin(n - (x ...

  2. (转)AIX 中 Paging Space 使用率过高的分析与解决

    AIX 中 Paging Space 使用率过高的分析与解决 原文:https://www.ibm.com/developerworks/cn/aix/library/au-cn-pagingspac ...

  3. SpringBoot 2.0中SpringWebContext 找不到无法使用的问题解决

    为了应对在SpringBoot中的高并发及优化访问速度,我们一般会把页面上的数据查询出来,然后放到redis中进行缓存.减少数据库的压力. 在SpringBoot中一般使用 thymeleafView ...

  4. .net core 的跨域

    .net core 的跨域问题花了 我很长时间 接下来我简单的描述下解决过程 首先我在前端用jquery的ajax去调用自己的本地接口大致如下 $.ajax({ type:"POST&quo ...

  5. If people in the communications only think about gains and losses of interest, then the pleasure of knowing each other will cease to exist.

    If people in the communications only think about gains and losses of interest, then the pleasure of ...

  6. 关于ev||event事件对象的兼容写法

    因为FireFox Chrome默认都是有一个值传进来的,所以这里是对IE和FireFox Chrome做了兼容. 例如:页面点击事件事件要使用document,获取鼠标位置 document.onc ...

  7. 1.2 the structure of a compiler

    Compiler 1.2 the structure  of a compiler Compiler : analysis and synthesis syntactically  语法上的 sema ...

  8. Arria II GX FPGA开法套件——初步使用

    1. 从官网下载使用手册和参考手册,以及开发包           下载地址:https://www.altera.com.cn/products/boards_and_kits/dev-kits/a ...

  9. SAP OData编程指南

    OData(Open Data Protocol)协议是一个开放的工业标准,用于定义RESTFul API的设计和使用.我的文章标题前加上SAP的前缀,只是为了表明这篇文章介绍的是Jerry在SAP项 ...

  10. TIF转JPG

    public void TifToJpg(string tifPath, string tifName) { try { //找到后缀为TIF的图像,如果没有,就catch退出 int len = t ...