Hit 2255 Not Fibonacci

简介:

今天下午刚起来眼睛就比较涨,,而且还有点恶心,唉,结果一直不在状态,而且这个题太坑了。。。。
点击此处即可传送 Hit 2255

Maybe ACMers of HIT are always fond of fibonacci numbers, because it is so beautiful. Don't you think so? At the same time, fishcanfly always likes to change and this time he thinks about the following series of numbers which you can guess is derived from the definition of fibonacci number. 

The definition of fibonacci number is: 

f(0) = 0, f(1) = 1, and for n>=2, f(n) = f(n - 1) + f(n - 2) 

We define the new series of numbers as below: 

f(0) = a, f(1) = b, and for n>=2, f(n) = p*f(n - 1) + q*f(n - 2),where p and q are integers. 

Just like the last time, we are interested in the sum of this series from the s-th element to the e-th element, that is, to calculate ."""" 

Great!Let's go! 

Input 

The first line of the input file contains a single integer t (1 <= t <= 30), the number of test cases, followed by the input data for each test case. 

Each test case contains 6 integers a,b,p,q,s,e as concerned above. We know that -1000 <= a,b <= 1000,-10 <= p,q <= 10 and 0 <= s <= e <= 2147483647. 

Output 

One line for each test case, containing a single interger denoting S MOD (10^7) in the range [0,10^7) and the leading zeros should not be printed. 

Sample Input 
2
0 1 1 -1 0 3
0 1 1 1 2 3


Sample Output 
2
3

题目大意:
就是给你好几个数,分别表示什么意思,看题就行了;

解题思路:矩阵乘法,递推公式,

注意了,注意了,千万不要用全局变量
if(ans < 0)
ans += mod;
printf(“%lld\n”,ans);
}
return 0;
}
!!!!!!!

/*
2015 - 8 - 14 下午
Author: ITAK

今天非常非常的不顺心啊。。。。

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 3;
const int mod = 1e7;
typedef long long LL;
typedef struct
{
    LL m[maxn][maxn];
} Matrix;
// LL a, b, s, e, q, p;千万不要用全局变量
Matrix P = {0,0,0,
            1,0,0,
            0,0,1
           };
Matrix I = {1,0,0,
            0,1,0,
            0,0,1
           };
Matrix matrix_mul(Matrix a, Matrix b)
{
    int i, j, k;
    Matrix c;
    for(i=0; i<maxn; i++)
    {
        for(j=0; j<maxn; j++)
        {
            c.m[i][j] = 0;
            for(k=0; k<maxn; k++)
            {
                a.m[i][k] = (a.m[i][k]%mod + mod) % mod;
                b.m[k][j] = (b.m[k][j]%mod + mod) % mod;
                c.m[i][j] += (a.m[i][k] * b.m[k][j]) % mod;
            }
            c.m[i][j] = (c.m[i][j]%mod + mod) % mod;
        }
    }
    return c;
}

Matrix quick_mod(LL m)
{
    Matrix ans = I, b = P;
    while(m)
    {
        if(m & 1)
            ans = matrix_mul(ans, b);
        m >>= 1;
        b = matrix_mul(b, b);
    }
    return ans;
}
int main()
{
    int t;
    LL a, b, q, p, e, s;
    scanf("%d",&t);
    while(t--)
    {


        Matrix tmp1, tmp2;
        LL ans, ans1, ans2;
        cin>>a>>b>>p>>q>>s>>e;
        P.m[0][0]=p;
        P.m[0][1]=q;
        P.m[2][0]=p;
        P.m[2][1]=q;
        if(s-2 > 0)
        {
            tmp1 = quick_mod(s-2);
            ans1 = (b*tmp1.m[2][0])%mod + (a*tmp1.m[2][1])%mod + ((a+b)*tmp1.m[2][2])%mod;
        }
        else
        {
            if(s == 0)
                ans1 = 0;
            if(s == 1)
                ans1 = a;
            if(s == 2)
                ans1 = a + b ;
        }
        if(e-1 > 0)
        {
            tmp2 = quick_mod(e-1);
            ans2 = (b*tmp2.m[2][0])%mod + (a*tmp2.m[2][1])%mod + ((a+b)*tmp2.m[2][2])%mod;
        }
        else
        {
            if(e == 0)
                ans2 = a;
            else
                ans2 = b+a;
        }
        ans1 = (ans1%mod+mod) % mod;
        ans2 = (ans2%mod+mod) % mod;
        //cout<<ans1<<" "<<ans2<<" ";
        ans = (ans2 - ans1 + mod) % mod;
        if(ans < 0)
            ans += mod;
        printf("%lld\n",ans);
    }
    return 0;
}
目录
相关文章
A. Hit the Lottery(贪心)
A. Hit the Lottery(贪心)
61 0
|
存储 Python
LeetCode 315. Count of Smaller Numbers After Self
给定一个整数数组 nums,按要求返回一个新数组 counts。数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于 nums[i] 的元素的数量。
94 0
LeetCode 315. Count of Smaller Numbers After Self
LeetCode 209. Minimum Size Subarray Sum
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。
116 0
LeetCode 209. Minimum Size Subarray Sum
|
安全
Hit the Lottery
Hit the Lottery
103 0
Hit the Lottery
Maximum Subsequence Sum
最大连续子列和问题,在此给出题解 (浙大PTA https://pintia.cn/problem-sets/16/problems/665)
ZCMU - 1990: Fibonacci
ZCMU - 1990: Fibonacci
79 0
|
存储 Java 测试技术
Next Fibonacci Number(下一个斐波拉契数列)
Write a program that takes input of integer N, followed by N more integers. For each integer, output the next fibonacci number after it.
1241 0
1104. Sum of Number Segments (20) consecutive subsequence 每个数出现的次数
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence.
967 0
|
人工智能 机器学习/深度学习
1007. Maximum Subsequence Sum (25)
简析:求最大子列和,并输出其首末元素。在线处理,关键在于求首末元素。 本题囧,16年9月做出来过,现在15分钟只能拿到22分,有一个测试点过不了。
974 0