zoukankan      html  css  js  c++  java
  • HDU 6030 Happy Necklace【矩阵快速幂】

    Happy Necklace

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 477    Accepted Submission(s): 198


    Problem Description
    Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of multiple red and blue beads.
    Little Q desperately wants to impress his girlfriend, he knows that she will like the necklace only if for every prime length continuous subsequence in the necklace, the number of red beads is not less than the number of blue beads.
    Now Little Q wants to buy a necklace with exactly n beads. He wants to know the number of different necklaces that can make his girlfriend happy. Please write a program to help Little Q. Since the answer may be very large, please print the answer modulo 109+7.
    Note: The necklace is a single string, {not a circle}.
     

    Input
    The first line of the input contains an integer T(1T10000), denoting the number of test cases.
    For each test case, there is a single line containing an integer n(2n1018), denoting the number of beads on the necklace.
     

    Output
    For each test case, print a single line containing a single integer, denoting the answer modulo 109+7.
     

    Sample Input
    2 2 3
     

    Sample Output
    3 4
     

    Source


    因为2是最小的素数,考虑长度为2的子串。红色为A,蓝色为B,则只有AA,AB,BA三种情况。对每种情况,在后面加上A或B,AA可以形成AA,AB,AB可以形成BA,BA可以形成AA。通过这个递推扩展到长度为n的情况,用矩阵快速幂加速即可。矩阵为:


    初始情况下,AA,AB,BA都有可能,因此最后将矩阵中的所有数字相加就是答案。


    注意n为10的18次方会爆int所以在矩阵快速幂的时候要用long longMatrix quickpow(Matrix A,ll k)】


    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    
    #define INF 0x3f3f3f3f
    
    #define mod 1000000007
    
    using namespace std;
    
    typedef long long ll;
    const int maxn = 100010;
    
    ll n;
    
    struct Matrix {
    	ll a[5][5];
    };
    
    
    Matrix mul(Matrix x, Matrix y)
    {
    	Matrix temp;
    	for (int i = 1; i <= 3; i++)
    		for (int j = 1; j <= 3; j++) temp.a[i][j] = 0;
    
    	for (int i = 1; i <= 3; i++)
    	{
    		for (int j = 1; j <= 3; j++)
    		{
    			ll sum = 0;
    			for (int k = 1; k <= 3; k++)
    			{
    				sum = (sum + x.a[i][k] * y.a[k][j] % mod) % mod;
    			}
    			temp.a[i][j] = sum;
    		}
    	}
    	return temp;
    }
    
    Matrix quickpow(Matrix A,ll k)
    {
    	Matrix res;
    	res.a[1][1] = 1; res.a[1][2] = 0; res.a[1][3] = 0;
    	res.a[2][1] = 0; res.a[2][2] = 1; res.a[2][3] = 0;
    	res.a[3][1] = 0; res.a[3][2] = 0; res.a[3][3] = 1;
    	while (k)
    	{
    		if (k & 1) res = mul(res, A);
    		A = mul(A, A);
    		k >>= 1;
    	}
    	return res;
    }
    
    int main()
    {
    	int t;
    	scanf("%d", &t);
    	while (t--)
    	{
    		scanf("%lld", &n);
    		if (n == 2)
    		{
    			printf("3
    ");
    			continue;
    		}
    		Matrix A;
    		A.a[1][1] = 1; A.a[1][2] = 0; A.a[1][3] = 1;
    		A.a[2][1] = 1; A.a[2][2] = 0; A.a[2][3] = 0;
    		A.a[3][1] = 0; A.a[3][2] = 1; A.a[3][3] = 0;
    		Matrix res = quickpow(A, n - 2);
    		ll x = (res.a[1][1] + res.a[1][2] + res.a[1][3]) % mod;
    		ll y = (res.a[2][1] + res.a[2][2] + res.a[2][3]) % mod;
    		ll z = (res.a[3][1] + res.a[3][2] + res.a[3][3]) % mod;
    		printf("%lld
    ", (x + y + z) % mod);
    	}
    }


    1018
    101Matrix quickpow(Matrix A,ll k)1018101810181018101810181018101810

    1018
    1018
    1018
    1018
    Fighting~
  • 相关阅读:
    using 关键字在 C++ 中的几种用法
    Chromium 修改图片资源
    SAM&广义SAM
    Burnside和Polya
    笔记:杜教筛
    笔记:莫比乌斯反演
    Miller-Rabin
    点分治
    虚树
    计算几何
  • 原文地址:https://www.cnblogs.com/Archger/p/12774770.html
Copyright © 2011-2022 走看看