您的位置:首页 > 其它

1052 - String Growth

2016-06-16 21:25 302 查看
1052 - String Growth

PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 32 MB
Zibon just started his courses in Computer science. After having some lectures on programming courses he fell in love with strings. He started to play with strings and experiments on them. One day he started a string of arbitrary (of course positive) length consisting of only {a, b}. He considered it as 1st string and generated subsequent strings from it by replacing all the b's with ab and all the a's with b. For example, if he ith string is abab, (i+1)th string will be b(ab)b(ab) = babbab. He found that the Nth string has length X and Mth string has length Y. He wondered what will be length of the Kth string. Can you help him?

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case begins with five integers N, X, M, Y, K. (0 < N, M, X, Y, K < 109 and N ≠ M).

Output

For each case print one line containing the case number and L which is the desired length (mod 1000000007) or the string "Impossible" if it's not possible.

Sample Input

Output for Sample Input

2

3 16 5 42 6

5 1 6 10 9

Case 1: 68

Case 2: Impossible

PROBLEM SETTER: MD. TOWHIDUL ISLAM TALUKDER
SPECIAL THANKS: JANE ALAM JAN
思路:矩阵快速幂;
感觉这道题的题意有点问题,所给你的条件不知道是否取模,不过最后错了好几次,,和样例可以确定是没取模。
那么说下思路:Fa(n+1)=Fb(n);Fb (n+1)=Fa(n)+Fb(n);



然后给你两个条件,那么你可以设fa(1)=x;fb(1)=y;然后解方程组,然后判定下方程是否有解,其中x>=0&&y>=0;其中系数用矩阵快速幂算下。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<vector>
using namespace std;
typedef long long LL;
const  LL mod=1e9+7;
LL quick1(LL n,LL m)
{
LL a=1;
while(m)
{
if(m&1)
{
a=(a*n)%mod;
}
n=(n*n)%mod;
m/=2;
}
return a;
}
typedef struct pp
{
LL m[3][3];
pp()
{
memset(m,0,sizeof(m));
}
} maxtr;
void Init(maxtr *ans)
{
int i,j,k;
for(i=0; i<=1; i++)
{
for(j=0; j<=1; j++)
{
if(i==0&&j==0)
{
ans->m[i][j]=0;
}
else if(i==1&&j==1)
{
ans->m[i][j]=1;
}
else ans->m[i][j]=1;
}
}
}
maxtr E()
{
maxtr ans;
int i,j;
for(i=0; i<=1; i++)
{
for(j=0; j<=1; j++)
{
if(i==j)
{
ans.m[i][j]=1;
}
else ans.m[i][j]=0;
}
}
return ans;
}
maxtr quick( maxtr ans,LL m)
{
maxtr cc=E();
while(m)
{
if(m&1)
{
maxtr ak;
int s;
int i,j;
for(i=0; i<=1; i++)
{
for(j=0; j<=1; j++)
{
for(s=0; s<=1; s++)
{
ak.m[i][j]=(ak.m[i][j]+(cc.m[s][j]*ans.m[i][s])%mod)%mod;
}
}
}
cc=ak;
}
maxtr ak;
int s;
int i,j;
for(i=0; i<=1; i++)
{
for(j=0; j<=1; j++)
{
for(s=0; s<=1; s++)
{
ak.m[i][j]=(ak.m[i][j]+(ans.m[i][s]*ans.m[s][j])%mod)%mod;
}
}
}
ans=ak;
m/=2;
}
return cc;
}
LL gcd(LL n, LL m)
{
if(m==0)
{
return n;
}
else if(n%m==0)
{
return m;
}
else return gcd(m,n%m);
}
int main(void)
{
LL i,j,k;
LL s;
LL  N, X, M, Y, K;
scanf("%d",&k);
LL acy;
LL acx;
for(s=1; s<=k; s++)
{
int flag=0;
scanf("%lld %lld %lld %lld %lld",&N,&X,&M,&Y,&K);
if(N>M)
{
swap(N,M);
swap(X,Y);
}
maxtr ak;
Init(&ak);
maxtr ac=quick(ak,N-1);
maxtr bk;
Init(&bk);
maxtr aak=quick(bk,M-1);
LL xx1=(ac.m[0][0]+ac.m[1][0])%mod;
LL yy1=(ac.m[0][1]+ac.m[1][1])%mod;
LL xx2=(aak.m[0][0]+aak.m[1][0])%mod;
LL yy2=(aak.m[0][1]+aak.m[1][1])%mod;
//printf("%lld %lld\n",xx1,xx2);
//printf("%lld %lld\n",yy1,yy2);
LL ccy=yy1;
LL xxN=X;
LL t=X;
LL xxy=yy2;
LL ct=Y;
LL yyM=Y;
LL gc=gcd(xx1,xx2);
yy1*=(xx2/gc);
yy2*=(xx1/gc);
X*=(xx2/gc);
Y*=(xx1/gc);
yy1-=yy2;
X-=Y;
if(X<0&&yy1>0||X>0&&yy1<0)flag=1;
{
if(X==0)
{
acy=0;
if(xxN%xx1)
{
flag=1;
}
else
{
LL ack=quick1(xx1,mod-2);
acx=xxN*ack%mod;
}
}
else
{

if(X%yy1)
{
flag=1;
}
else
{   if(yy1>0&&X<0||yy1<0&&X>0)flag=1;
LL ack=quick1(yy1,mod-2);
acy=X/yy1%mod;
xxN-=ccy*acy;
if(xxN<0)flag=1;
if(xxN%xx1)
{
flag=1;
}
else
{
LL ack=quick1(xx1,mod-2);
acx=xxN/xx1;
}
}
}

}
printf("Case %d: ",s);
if(flag)
{
printf("Impossible\n");
}
else
{
maxtr cp;
Init(&cp);
maxtr akk=quick(cp,K-1);
LL xxx1=(akk.m[0][0]+akk.m[1][0])*acx%mod;
LL yyy1=(akk.m[0][1]+akk.m[1][1])*acy%mod;
printf("%lld\n",(xxx1+yyy1)%mod);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: