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. python剑指offer 包含min函数的栈

    题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). # -*- coding:utf-8 -*- class Solution: def ...

  2. ubuntu install oracle jdk

    .Download the required tarball from here .unzip this tarball using "tar -zxvf tarball_name .cre ...

  3. WP Mail SMTP插件解决Contact Form 7表单提交失败问题

    WP Mail SMTP插件解决Contact Form 7表单提交失败问题 WP Mail SMTP是一款非常优秀的解决WordPress主机因为不支持或者是禁用了mail()函数,导致无法实现在线 ...

  4. react与微信小程序

    由组员完成 原文链接 都说react和微信小程序很像,但是像在什么部分呢,待我稍作对比. 生命周期 1.React React的生命周期在16版本以前与之后发生了重大变化,原因在于引入的React F ...

  5. <%%>用法初步认识

    <%%>是用于向客户端插入服务器代码所使用的一种标记 例如为了在HTML页面上展示由服务器提供的当前用户的某条信息或名字等便可使用 前台 <a href="home.asp ...

  6. Python知识点进阶——迭代器

    可迭代对象 可迭代对象可以简单的理解为用for循环遍历的,如list.tuple.dict.set.str 判断一个对象是否是迭代器: 可以将数据类型 和 是否为可迭代对象 比较来判断是否是可以迭代 ...

  7. python练手习题

    不断记录python常见习题,不断寻求更多更好的解决办法.持续更新中..... 练习: 1. list两两元素交换位置,如[1,2,3,4,5,6] 执行后为 -> [2,1,4,3,6,5] ...

  8. python3 爬取汽车之家所有车型数据操作步骤(更新版)

    题记: 互联网上关于使用python3去爬取汽车之家的汽车数据(主要是汽车基本参数,配置参数,颜色参数,内饰参数)的教程已经非常多了,但大体的方案分两种: 1.解析出汽车之家某个车型的网页,然后正则表 ...

  9. C++构造函数实例——拷贝构造,赋值

    #define _CRT_SECURE_NO_WARNINGS //windows系统 #include <iostream> #include <cstdlib> #incl ...

  10. HDU1505-City Game(记忆化搜索)

    City Game http://acm.hdu.edu.cn/showproblem.php?pid=1505 Problem Description Bob is a strategy game ...