Google Code Jam 2008 Round 1A C Numbers(矩阵快速幂+化简方程,好题)
Problem C. Numbers
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, T. T 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
|
Case #1: 935 |
题目链接: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(矩阵快速幂+化简方程,好题)的更多相关文章
- 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 ...
- 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. ...
- [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 ...
- [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 ...
- 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 ...
- hdu 3117 Fibonacci Numbers 矩阵快速幂+公式
斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...
- [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 ...
- [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 ...
- Count Numbers(矩阵快速幂)
Count Numbers 时间限制: 8 Sec 内存限制: 128 MB提交: 43 解决: 19[提交] [状态] [讨论版] [命题人:admin] 题目描述 Now Alice want ...
随机推荐
- 【luogu P1983 车站分级】 题解
题目链接:https://www.luogu.org/problemnew/show/P1983 符合了NOIP命题的特点,知识点不难,思维量是有的. step1:把题读进去,理解.得到 非停靠点的等 ...
- java常用 开源
http://sourceforge.nethttp://code.google.com/hosting/http://www.open-open.com/code/tags/Javahttp://w ...
- SQL数据库查询出一张表中重复的数据,按某个字段来查找。
例如表名为Course 需要查询出name重复的有那些??? 解答如下: 补充: 如:查询每个姓名出现大于2次,SQL如下 SELECT COUNT(NAME) as '出现次数', NAME FR ...
- ES6 Reflect使用笔记
Reflect Reflect 对象和Proxy对象一样, 为操作对象提供了新的API. 为什么使用 Reflect的方式来操作对象? 将 Object 对象上一些明显属于内部的方法放到 Reflec ...
- VS Code 用户自定义代码片段(React)
VS Code 用户自定义代码片段(React) .jsxReact组件模板:javascriptreact.json { "Import React": { "pref ...
- jquery的ajax请求
加载页面内容,如果不加选择器,会加载整个页面内容 加选择器会获取选择器内容 例如: <script> //可以获取json格式的文件 $.ajax({ type:"get&quo ...
- Everything Be True-freecodecamp算法题目
Everything Be True 1.要求 完善every函数,如果集合(collection)中的所有对象都存在对应的属性(pre),并且属性(pre)对应的值为真.函数返回ture.反之,返回 ...
- Maven和Gradle对比(转载)
转载出处:http://www.cnblogs.com/huang0925 Java世界中主要有三大构建工具:Ant.Maven和Gradle.经过几年的发展,Ant几乎销声匿迹.Maven也日薄西山 ...
- NoSQL - KVstore -Redis
Redis键迁移 在使用Redis的过程中,很多时候我们会遇到需要进行键迁移的问题,需要将指定Redis中的指定数据迁移到其他Redis当中,键迁移有三种方法,我们来进行一一介绍. 一.move mo ...
- Oracle 数据库密码过期问题
(1)在CMD命令窗口中输入: sqlplus 用户名/密码@数据库本地服务名 as sysdba;(如:sqlplus scott/1234@oracle1 as sysdba; ...