Description
小澳者表也,数学者景也,表动则景随矣。
小澳不喜欢数学,可数学却待小澳如初恋,小澳睡觉的时候也不放过。
小澳的梦境中出现了一个平面直角坐标系,自原点,向四方无限延伸。
小澳在坐标系的原点,他可以向上、向左或者向右走。他可以走n步,但不能经过相同的点。
小澳想知道他有多少种走法。
Input
输入文件名为coordinate.in。
输入文件仅第一行一个正整数n,表示小澳可以走的步数。
Output
输出文件名为coordinate.out。
输出文件共一行,输出一个正整数,表示答案(对10^9+7取模)。
Sample Input
样例输入1:
2
样例输入2:
3
Sample Output
样例输出1:
7
样例输出2:
17
Data Constraint
测试点1~2
n<=10
测试点3~4
n<=100
测试点5~6
n<=1000
测试点7~8
n<=10^6
测试点9~10
n<=10^9
Hint
从(0,0)出发走2步,共7种走法:
(0,0)->(0,1)->(0,2)
(0,0)->(0,1)->(1,1)
(0,0)->(0,1)->(-1,1)
(0,0)->(1,0)->(2,0)
(0,0)->(1,0)->(1,1)
(0,0)->(-1,0)->(-2,0)
(0,0)->(-1,0)->(-1,1)
题解
DP式子十分显然我懒得打
快速幂优化十分显然我懒得打
贴代码
const md=1000000007;
var
ju:array[1..3,1..3]of int64=((1,1,1),
(1,1,0),
(1,0,1));
ju1:array[1..3]of int64;
c1:array[1..3,1..3]of int64;
i,j,k,l,n:longint;
x:int64;
procedure cheng;
var
i,j,k:longint;
c1:array[1..3]of int64;
begin
fillchar(c1,sizeof(c1),0);
for i:=1 to 3 do
for j:=1 to 3 do
c1[i]:=(c1[i]+ju1[j]*ju[j,i]) mod md;
ju1:=c1;
end;
procedure cheng1;
var
i,j,k:longint;
begin
fillchar(c1,sizeof(c1),0);
for i:=1 to 3 do
for j:=1 to 3 do
for k:=1 to 3 do
c1[i,j]:=(c1[i,j]+ju[i,k]*ju[k,j]) mod md;
ju:=c1;
end;
begin
assign(input,'coordinate.in'); reset(input);
assign(output,'coordinate.out'); rewrite(output);
readln(n);
l:=n-1;
for i:=1 to 3 do ju1[i]:=1;
while l>0 do
begin
if l mod 2=1 then cheng;
cheng1;
l:=l div 2;
end;
x:=(ju1[1]+ju1[2]) mod md;
x:=(x+ju1[3]) mod md;
writeln(x);
close(input); close(output);
end.