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 ...
随机推荐
- CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第八节
原文链接 第八节:利用CUDA函数库 Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Laboratory)的高级科研人员.他在多个国家级的实验室进 ...
- css与html结合四种方式
方式一:每个标签加一个属性法 <div style="background-color:red;color:green;"></div> 方式二:head中 ...
- 单例Singleton
先提供一个完整版: // .h文件 @interface SingleTon : NSObject /** 获取单例对象 */ + (instancetype)sharedInstance; + (i ...
- SAP销售订单屏幕增强行项目屏幕增强
1.在vbap表中 append一个自定义结构,如下图: 2.TCODE:SE80 程序名:SAPMV45A 屏幕:8459 如图: 3.标记增强的屏幕字段 4.屏幕增强的位置 *& ...
- mysql update 多表关联更新
UPDATE new_schedules_spider_static_schedule s join new_scac_port p on p.`PORT` = s.`PORT` and p.SCAC ...
- django+xadmin在线教育平台(四)
3-2 配置表单页面 必要的该说的,该了解的 前置条件: 你已经学习了前面教程.将项目的文件夹目录结构,setting配置等修改完毕与我保持一致. 本节通过Django快速的配置一个留言板页面来学习 ...
- Linux 系统性能
Linux:PS命令详解与使用 要对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程,ps命令就是最基本进程查看命令.使用该命令可以确定有哪些进程正在运行和运行的状态.进程 ...
- 第二章JavaScript 函数和对象
1 JavaScript 函数 1.1 声明函数的方式 function 关键字 匿名函数方式(表达式方式) Function 构造函数方式 1.2 参数问题 形参和实参数量问题 可选形参(参数默认值 ...
- ATM-lib-common
import logging.configfrom conf import settingsfrom core import src def get_logger(name): logging.con ...
- php v8js
本文整理自大神 Corz 1.php56 /datas/soft/php56/bin/php -v PHP (cli) #https://blog.csdn.net/lzm198707/article ...