【矩阵快速幂】南昌邀请赛 C Angry FFF Party

https://nanti.jisuanke.com/t/38222

In ACM labs, there are only few members who have girlfriends. And these people can make FFF Party angry easily. One day, FFF Party prepared a function Fand a set S.

There are several unequal positive integers fi​ in the set S.

They calculated the value of W=∑f∈S​F(F(f)) , and tried to cause W damage to those who have girlfriends. Suppose you are not a single dog and have been attacked. Now, give you the value of W and please write a program to calculate these fi​ in set S.

Input

The first line consists of a single integer T denoting the number of test cases.

TT lines follow, with an integer in each, denoting the result of W.

Output

For each test case, print a sequence of space-separated integers fi​ satisfying the equation.

If there are more than one answers, please print the lexicographically smallest one.

If there’s no such sequence, print -1 instead.

Constraints

1≤T≤10

1≤W≤10100,000

样例输入

2
1
3

样例输出

1
1 2 3

题意:问你斐波那契套斐波那契能否用这些数加起来组成W

解法:因为斐波那契套斐波那契的第29项就到了10的10万次方,那么只要矩阵快速幂预处理处斐波那契套斐波那契的第1-29项

从大到小遍历W能减则减就行  W<=10时要特殊判断,因为要输出字典序最小

所以比如3 可以是1 2 3 也可以是1 4,但字典序小是1 2 3

import java.math.BigInteger;
import java.util.Scanner;



public class Main {
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner cin = new Scanner(System.in);
		int H[] = new int [40];
		BigInteger Ans[]=new BigInteger [40];
		H[1]=H[2]=1;
		for(int i=3;i<=29;i++)
		{
			H[i]=H[i-1]+H[i-2];
		}
		
		for(int i=1;i<=29;i++)
		{
			int n = H[i];
			n -= 1;//
			BigInteger a[][] = { { BigInteger.valueOf(1), BigInteger.valueOf(1) }, { BigInteger.valueOf(1), BigInteger.valueOf(0) } };
			BigInteger b[][] = { { BigInteger.valueOf(1), BigInteger.valueOf(0) }, { BigInteger.valueOf(0), BigInteger.valueOf(1) } };// 单位矩阵
			while (n > 0) {
				if (n % 2 == 1) {
					b = q(a, b);
				}
				a = q(a, a);
				n /= 2;
			}
			Ans[i]=b[0][0];
			//System.out.println(i+" "+Ans[i]);
		}
	
		int T=cin.nextInt();
		for(int t=1;t<=T;t++)
		{
			BigInteger s;
			s=cin.nextBigInteger();
			int num[]=new int [100];
			int cnt=0;
			for(int i=29;i>=6;i--)
			{
				if(s.compareTo(Ans[i])==1||s.compareTo(Ans[i])==0)
				{
					s=s.subtract(Ans[i]);
					num[++cnt]=i;
				}
			}
			
			if(s.compareTo(BigInteger.valueOf(10))==1)
			{
				System.out.println("-1");
				continue;
			}
			if(s.equals(BigInteger.valueOf(0)))
			{
				for(int i=cnt;i>=1;i--)
				{
					if(i==cnt)
						System.out.print(num[i]);
					else
						System.out.print(" "+num[i]);
				}
				System.out.println();
			}
			
			else	
			{
				if(s.equals(BigInteger.valueOf(1)))
				{
					System.out.print("1");
				}
				else if(s.equals(BigInteger.valueOf(2)))
				{
					System.out.print("1 2");
				}
				else if(s.equals(BigInteger.valueOf(3)))
				{
					System.out.print("1 2 3");
				}
				else if(s.equals(BigInteger.valueOf(4)))
				{
					System.out.print("1 2 4");
				}
				else if(s.equals(BigInteger.valueOf(5)))
				{
					System.out.print("1 2 3 4");
				}
				else if(s.equals(BigInteger.valueOf(6)))
				{
					System.out.print("1 5");
				}
				else if(s.equals(BigInteger.valueOf(7)))
				{
					System.out.print("1 2 5");
				}
				else if(s.equals(BigInteger.valueOf(8)))
				{
					System.out.print("1 2 3 5");
				}
				else if(s.equals(BigInteger.valueOf(9)))
				{
					System.out.print("1 2 4 5");
				}
				else if(s.equals(BigInteger.valueOf(10)))
				{
					System.out.print("1 2 3 4 5");
				}
				for(int i=cnt;i>=1;i--)
				{
					System.out.print(" "+num[i]);
				}
				System.out.println();
			}
			
		}
		
	}

	static BigInteger[][] q(BigInteger[][] a, BigInteger[][] b) {//
		BigInteger value1 = (a[0][0].multiply( b[0][0] ) ).add( a[0][1] .multiply(b[1][0]));// 左上
		BigInteger value2 = (a[0][0].multiply( b[0][1] ) ).add( a[0][1] .multiply(b[1][1]));// 左上
		BigInteger value3 =(a[1][0].multiply( b[0][0] ) ).add( a[1][1] .multiply(b[1][0]));// 左上
		BigInteger value4 = (a[1][0].multiply( b[0][1] ) ).add( a[1][1] .multiply(b[1][1]));// 左上
		BigInteger c[][] = new BigInteger[2][2];
		c[0][0] = value1 ;
		c[0][1] = value2 ;
		c[1][0] = value3;
		c[1][1] = value4;
		return c;
	}


}

猜你喜欢

转载自blog.csdn.net/qq_41037114/article/details/89433324
fff
今日推荐