Problem C. Numbers

This contest is open for practice. You can try every problem as many times as you like, though we won't keep track of which problems you solve. Read the Quick-Start Guide to get started.
Small input
15 points

Solve C-small

 

Large input
35 points

Solve C-large

 

Problem

In this problem, you have to find the last three digits before the decimal point for the number (3 + √5)n.

For example, when n = 5, (3 + √5)5 = 3935.73982... The answer is 935.

For n = 2, (3 + √5)2 = 27.4164079... The answer is 027.

Input

The first line of input gives the number of cases, TT test cases follow, each on a separate line. Each test case contains one positive integer n.

Output

For each input case, you should output:

Case #X: Y

where X is the number of the test case and Y is the last three integer digits of the number (3 + √5)n. In case that number has fewer than three integer digits, add leading zeros so that your output contains exactly three digits.

Limits

1 <= T <= 100

Small dataset

2 <= n <= 30

Large dataset

2 <= n <= 2000000000

Sample


Input 
 

Output 
 
2
5
2
Case #1: 935
Case #2: 027

题目链接:Problem C. Numbers

挑战编程书上的题目,跟HDU的4565有点像,只是这题a与b固定,要求的是整数部分的最后三位数字,不足补0。

一开始书上的公式看不懂,问了下同学才弄懂,可以参考草稿纸上写的推出$a_n$的公式

按照这个思路可以知道题目中所求的答案就是$2*a_n-1$了

然后构造矩阵去求$a_n$即可

代码:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 2;
int a = 3, b = 5, n, m = 1000; int mul(int a, int b)
{
int r = 0;
while (b)
{
if (b & 1)
r = (r + a) % m;
a = (a << 1) % m;
b >>= 1;
}
return r;
}
struct Mat
{
int A[N][N];
void zero()
{
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
A[i][j] = 0;
}
void one()
{
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
A[i][j] = (i == j);
}
Mat operator*(Mat b)
{
Mat c;
c.zero();
for (int k = 0; k < N; ++k)
{
for (int i = 0; i < N; ++i)
{
if (A[i][k])
{
for (int j = 0; j < N; ++j)
{
if (b.A[k][j])
c.A[i][j] = (c.A[i][j] + mul(A[i][k], b.A[k][j])) % m;
}
}
}
}
return c;
}
friend Mat operator^(Mat a, int b)
{
Mat r;
r.one();
while (b)
{
if (b & 1)
r = r * a;
a = a * a;
b >>= 1;
}
return r;
}
};
int main(void)
{
while (~scanf("%d", &n))
{
Mat A, B;
A.zero();
B.zero();
A.A[0][0] = 1; A.A[0][1] = 0;
A.A[1][0] = 1; A.A[1][1] = 0;
B.A[0][0] = a; B.A[0][1] = 1;
B.A[1][0] = b; B.A[1][1] = a;
A = A * (B ^ n);
printf("Case #%d: %03d\n", q, ((A.A[0][0] << 1) - 1) % m);
}
return 0;
}

Google Code Jam 2008 Round 1A C Numbers(矩阵快速幂+化简方程,好题)的更多相关文章

  1. Google Code Jam 2010 Round 1A Problem A. Rotate

    https://code.google.com/codejam/contest/544101/dashboard#s=p0     Problem In the exciting game of Jo ...

  2. Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)

    Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...

  3. [C++]Saving the Universe——Google Code Jam Qualification Round 2008

    Google Code Jam 2008 资格赛的第一题:Saving the Universe. 问题描述如下: Problem The urban legend goes that if you ...

  4. [C++]Store Credit——Google Code Jam Qualification Round Africa 2010

    Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...

  5. Google Code Jam 2010 Round 1C Problem A. Rope Intranet

    Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...

  6. hdu 3117 Fibonacci Numbers 矩阵快速幂+公式

    斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...

  7. [Google Code Jam (Qualification Round 2014) ] B. Cookie Clicker Alpha

    Problem B. Cookie Clicker Alpha   Introduction Cookie Clicker is a Javascript game by Orteil, where ...

  8. [Google Code Jam (Qualification Round 2014) ] A. Magic Trick

    Problem A. Magic Trick Small input6 points You have solved this input set.   Note: To advance to the ...

  9. Count Numbers(矩阵快速幂)

    Count Numbers 时间限制: 8 Sec  内存限制: 128 MB提交: 43  解决: 19[提交] [状态] [讨论版] [命题人:admin] 题目描述 Now Alice want ...

随机推荐

  1. 使用canvas给图片添加水印

    css部分 .clip { position: absolute; clip: rect(0 0 0 0); } html部分 <input type="file" id=& ...

  2. edge不能上网-代码 INET_E_RESOURCE_NOT_FOUND

    这个问题 ,网上有很多解决方法,我基本都测试了一遍,可是我都没有用 情况:首先,我开始的时候是可以用的,然后在公司,开了代理,就不能使用了,这是我之后多次尝试发现的,所以你也遇到和我一样的情况不必惊慌 ...

  3. 将指定的form表单所有输入项转为json数据

    今天学习时,看到的将form表单中的输入数据转成json 的jquery代码,直接贴出来: $.fn.serializeJson=function(){ var serializeObj={}; va ...

  4. JDBC开发,数据库的连接

    JDBC开发步骤 1.建完工程后,右击工程,new,新建一个文件夹Folder,装jar包,        2.将下载好的驱动包打开,找到jar文件,CTRL+C,选中装jar包的文件夹,CTRL+V ...

  5. XGBoost算法原理小结

    在两年半之前作过梯度提升树(GBDT)原理小结,但是对GBDT的算法库XGBoost没有单独拿出来分析.虽然XGBoost是GBDT的一种高效实现,但是里面也加入了很多独有的思路和方法,值得单独讲一讲 ...

  6. jQuery Pagination分页插件--无刷新

    源码:https://github.com/SeaLee02/FunctionModule/blob/master/UploadFiles/WebDemo/FenYE/FenYeAjax.aspx 代 ...

  7. c#树形结构

    http://download.csdn.net/detail/rememberme001/6997235 http://bbs.csdn.net/topics/310094150 http://ww ...

  8. 适配iOS10和Xcode8

    1.权限设置 iOS10,访问系统权限需要在info.plist中注册,否则直接crash! 注意,Value值不可为空,否则会被Appstore拒掉! 2.Notification,学习资料 喵神总 ...

  9. 项目实战8.1—tomcat企业级Web应用服务器配置与会话保持

    分类: Linux架构篇   tomcat企业级Web应用服务器配置与实战 环境背景:公司业务经过长期发展,有了很大突破,已经实现盈利,现公司要求加强技术架构应用功能和安全性以及开始向企业应用.移动A ...

  10. phpspider案例

    phpspider案例 <?php require './autoload.php'; use phpspider\core\phpspider; /* Do NOT delete this c ...