ACM-ICPC 2015 Shenyang Preliminary Contest B. Best Solver
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Samara SAU ACM ICPC 2013-2014 Quarterfinal Qualification Contest
A: 简单题,因为题目中说了不会有数据相同: #include<cstdio> #include<algorithm> #define maxn 200005 using na ...
随机推荐
- 041 First Missing Positive 第一个缺失的正数
给一个未排序的数组,找出第一个缺失的正整数.例如,[1,2,0] 返回 3,[3,4,-1,1] 返回 2.你的算法应该在 O(n) 的时间复杂度内完成并且使用常数量的空间.详见:https://le ...
- 详解Java构造方法为什么不能覆盖,我的钻牛角尖病又犯了....
一 看Think in Java,遇到个程序 class Egg2 { protected class Yolk { public Yolk() { System.out.println(" ...
- 一步步实现自己的ORM(二)
在第一篇<一步步实现自己的ORM(一)>里,我们用反射获取类名.属性和值,我们用这些信息开发了简单的INSERT方法,在上一篇文章里我们提到主键为什么没有设置成自增长类型,单单从属性里我们 ...
- SQL判断一个事件段 是否在数据库中与其他时间段有重叠 判断时间重叠
数据库字段startDate 开始时间 endDate 结束时间 -两个参数 比如查2-2 至2-6 在数据库中是否与其他时间有重叠 四个条件有一项满足则有重叠时间 思路是这样子 以开始和结束 ...
- Appium基础二:Appium的安装(基Windows)
1.JAVA环境配置: 1.1安装jdk: 1.2配置JAVA_Home.Path配置.java验证 Path: 输入C:\Program Files\Java\jdk1.8.0_121\bin:C: ...
- getuser
Help on function getuser in module getpass: getuser() Get the username from the environment or pa ...
- Python+selenium 之操作Cookie
在验证浏览器中cookie是否正确时,有时基于真实cookie的测试是无法通过白盒和集成测试进行的.Webdriver提供了操作Cookie的相关方法,可以读取,添加和删除cookie信息. 文本we ...
- 如何远程连接Windows server上的MySQL服务
废话不多说,直接开干 首先要打开服务器的MySQL端口号:3306(当然,也可以把服务器的防火墙直接关闭,不过不安全) 1.打开服务器管理器,有个高级安全Windows防火墙,下面有一个入站规则, 右 ...
- Forbidden You don't have permission to access /phpStudyTest/application/index/controller/Index.php on this server.
发生情况:将thinkPHP从官网上下了 http://thinkphp.cn 然后安装了phpstudy和PHPstorm,并将thinkPHP解压到www路径下 在用PHPstorm打开 thi ...
- TreeMap 底层是红黑树 排序是根据key值进行的 添加元素时异常 Comparable异常 Comparator比较自定义对象放在键的位置
package com.swift; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; ...