CSU 1413: Area of a Fractal(分形图形面积)

题目:

S  tarting from   the   s  tring   F+F+F+F,   replace every F by   F+F-F-FF+FF  simultaneously per iteration,   then the string will be changed as shown below  :
  = 0 : F+F+F+F
n   = 1 : F+F-F-FF+FF+F+F-F-FF+FF+F+F-F-FF+FF+F+F-F-FF+FF
  = 2 : F+F-F-FF+FF+F+F-F-FF+FF-F+F-F-FF+FF-F+F-F-FF+FFF+F-F-FF+FF+F+F-F-FF+FFF+F-F-FF+FF+F+F-F-FF+FF+F+F-F-FF+FF-F+F-F-FF+FF-F+F-F-FF+FFF+F-F-FF+FF+F+F-F-FF+FFF+F-F-FF+FF+F+F-F-FF+FF+F+F-F-FF+FF-F+F-F-FF+FF-F+F-F-FF+FFF+F-F-FF+FF+F+F-F-FF+FFF+F-F-FF+FF+F+F-F-FF+FF+F+F-F-FF+FF-F+F-F-FF+FF-F+F-F-FF+FFF+F-F-FF+FF+F+F-F-FF+FFF+F-F-FF+FF
...
Here   n   means the number of iterations.
 
Indeed, each symbol has its  specific meaning: F means “draw forward 1 unit”, + means “turn left 90 degrees”, and - means “turn right 90 degrees”. So every string produced can be drawn as a shape ( Starting from the origin in the positive direction of x-axis  ).
For example, the corresponding shapes when   n  equals 0 and 1 are as shown in the figure below.

Given the number of iterations   n  , calculate the area of the corresponding shape.
Input
The first   line   contains the number of test cases   T   (1 ≤   T   ≤ 200).
For each test case, there is only one line with an integer   n   (0 ≤   n   ≤ 10  9 ), giving the number of iterations.
Output
For each test case, output an integer in one line, giving the area of the corresponding shape. Because the answer may be very large, you should just output the remainder when it divided by 1000000007.
Sample Input
3
0
1
2
Sample Output
1
13
193
Hint
Explanation of sample 3: The corresponding shape when   n  equals 2 is as shown in the figure below.


思路:

下列图片来自https://www.cnblogs.com/Empress/p/4152143.html


递推式:


公式:


5的逆元可以直接口算出来是400000003


代码:

#include<iostream>
#include<stdio.h>
using namespace std;

int p = 1000000007;

long long f7(int n)
{
	if (n == 0)return 1;
	long long r = f7(n / 2);
	r = (r*r) % p;
	if (n % 2)r *= 7;
	return r%p;
}

long long f17(int n)
{
	if (n == 0)return 1;
	long long r = f17(n / 2);
	r = (r*r) % p;
	if (n % 2)r *= 17;
	return r%p;
}

int main()
{
	int t, n;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &n);
		long long r = f7(n) * 2 + f17(n) * 3;
		r = r * 400000003 % p;
		printf("%lld\n", r);
	}
	return 0;
}

/**********************************************************************
	Problem: 1413
	User: 3901140225
	Language: C++
	Result: AC
	Time:8 ms
	Memory:2024 kb
**********************************************************************/


猜你喜欢

转载自blog.csdn.net/nameofcsdn/article/details/80255865
今日推荐