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. 048 Rotate Image 旋转图像

    给定一个 n × n 的二维矩阵表示一个图像.将图像旋转 90 度(顺时针).注意:你必须在原矩阵中旋转图像,请不要使用另一个矩阵来旋转图像.例 1:给出的输入矩阵 = [  [1,2,3],  [4 ...

  2. response返回字符床

    response.getWriter().println() 本来一个html,JSP等WEB资源返回的就是一个String,只是有时候这个String是符合html格式的,而刚是浏览器接收的了,所以 ...

  3. 六,IO系统

    六,IO系统 一,数据源 1,数据源--管道确认使用那根管道--节点流 2,先确定管道在tey中new出管道,new出后就写关闭代码,写完关闭代码在写中间代码 3,取数据和放数据结束语句必须有两个,不 ...

  4. java wait(),notify(),notifyAll()

    wait()的作用是使当前执行代码的线程进行等待,此方法是Object类的方法,该方法用来将当前线程置入“预执行队列”中,并且在wait()所带的代码处停止执行,直到接到通知或被中断位置.在调用wai ...

  5. 【0 基础学Dojo】第【1】篇 HelloWord

    打开dojo 官网首页 http://dojotoolkit.org/,我们看到 点击get dojo  你将得到下载Dojo 的不同方式 2,点击下面方式下载, 解压后 新建myTest.html, ...

  6. 使用Robot类模拟鼠标、键盘事件

    Robot类用于模拟鼠标.键盘事件,生成本机系统输入事件.Robot 的主要用于自动化.自运行的程序和其他需要自动控制鼠标和键盘的程序 相当于实际操作的效果,不仅仅只是生成对应的鼠标.键盘事件.比如R ...

  7. js控制语句

    1 条件判断语句 条件语句用于基于不同的条件来执行不同的动作. 1.1if 语句 if (condition){    当条件为 true 时执行的代码} 1.2if...else 语句 if (co ...

  8. cocos2d-x入门学习篇;切换场景

    手机游戏开发最近很火爆,鉴于一直在学习c++,看起来上手就比较快了.这篇文章来自皂荚花 cocos2d-x技术,我把我的想法分享给大家. 首先来看一段代码: CCScene* HelloWorld:: ...

  9. iOS NSDecimalNumber 使用

    在iOS开发中,经常遇到和货币价格计算相关的,这时就需要注意计算精度的问题.使用float类型运算,经常出现误差.为了解决这种问题我们使用NSDecimalNumber,下面将通过例子的形式给大家展示 ...

  10. 快速搭建高可用 LNMP Web应用基础架构

    云服务器费用:查看费用 产品详情                 产品介绍 本镜像是根据Azure Resource Manager模板创建的,基于资源组下的高可用web系统,主要包括前端web负载均 ...