1052 - String Growth
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);F(n+1)=Fa(n)+Fb(n);
 
然后给你两个条件,那么你可以设fa(1)=x;fb(1)=y;然后解方程组,然后判定下方程是否有解,其中x>=0&&y>=0;其中系数用矩阵快速幂算下。
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<math.h>
8 #include<vector>
9 using namespace std;
10 typedef long long LL;
11 const LL mod=1e9+7;
12 LL quick1(LL n,LL m)
13 {
14 LL a=1;
15 while(m)
16 {
17 if(m&1)
18 {
19 a=(a*n)%mod;
20 }
21 n=(n*n)%mod;
22 m/=2;
23 }
24 return a;
25 }
26 typedef struct pp
27 {
28 LL m[3][3];
29 pp()
30 {
31 memset(m,0,sizeof(m));
32 }
33 } maxtr;
34 void Init(maxtr *ans)
35 {
36 int i,j,k;
37 for(i=0; i<=1; i++)
38 {
39 for(j=0; j<=1; j++)
40 {
41 if(i==0&&j==0)
42 {
43 ans->m[i][j]=0;
44 }
45 else if(i==1&&j==1)
46 {
47 ans->m[i][j]=1;
48 }
49 else ans->m[i][j]=1;
50 }
51 }
52 }
53 maxtr E()
54 {
55 maxtr ans;
56 int i,j;
57 for(i=0; i<=1; i++)
58 {
59 for(j=0; j<=1; j++)
60 {
61 if(i==j)
62 {
63 ans.m[i][j]=1;
64 }
65 else ans.m[i][j]=0;
66 }
67 }
68 return ans;
69 }
70 maxtr quick( maxtr ans,LL m)
71 {
72 maxtr cc=E();
73 while(m)
74 {
75 if(m&1)
76 {
77 maxtr ak;
78 int s;
79 int i,j;
80 for(i=0; i<=1; i++)
81 {
82 for(j=0; j<=1; j++)
83 {
84 for(s=0; s<=1; s++)
85 {
86 ak.m[i][j]=(ak.m[i][j]+(cc.m[s][j]*ans.m[i][s])%mod)%mod;
87 }
88 }
89 }
90 cc=ak;
91 }
92 maxtr ak;
93 int s;
94 int i,j;
95 for(i=0; i<=1; i++)
96 {
97 for(j=0; j<=1; j++)
98 {
99 for(s=0; s<=1; s++)
100 {
101 ak.m[i][j]=(ak.m[i][j]+(ans.m[i][s]*ans.m[s][j])%mod)%mod;
102 }
103 }
104 }
105 ans=ak;
106 m/=2;
107 }
108 return cc;
109 }
110 LL gcd(LL n, LL m)
111 {
112 if(m==0)
113 {
114 return n;
115 }
116 else if(n%m==0)
117 {
118 return m;
119 }
120 else return gcd(m,n%m);
121 }
122 int main(void)
123 {
124 LL i,j,k;
125 LL s;
126 LL N, X, M, Y, K;
127 scanf("%d",&k);
128 LL acy;
129 LL acx;
130 for(s=1; s<=k; s++)
131 {
132 int flag=0;
133 scanf("%lld %lld %lld %lld %lld",&N,&X,&M,&Y,&K);
134 if(N>M)
135 {
136 swap(N,M);
137 swap(X,Y);
138 }
139 maxtr ak;
140 Init(&ak);
141 maxtr ac=quick(ak,N-1);
142 maxtr bk;
143 Init(&bk);
144 maxtr aak=quick(bk,M-1);
145 LL xx1=(ac.m[0][0]+ac.m[1][0])%mod;
146 LL yy1=(ac.m[0][1]+ac.m[1][1])%mod;
147 LL xx2=(aak.m[0][0]+aak.m[1][0])%mod;
148 LL yy2=(aak.m[0][1]+aak.m[1][1])%mod;
149 //printf("%lld %lld\n",xx1,xx2);
150 //printf("%lld %lld\n",yy1,yy2);
151 LL ccy=yy1;
152 LL xxN=X;
153 LL t=X;
154 LL xxy=yy2;
155 LL ct=Y;
156 LL yyM=Y;
157 LL gc=gcd(xx1,xx2);
158 yy1*=(xx2/gc);
159 yy2*=(xx1/gc);
160 X*=(xx2/gc);
161 Y*=(xx1/gc);
162 yy1-=yy2;
163 X-=Y;
164 if(X<0&&yy1>0||X>0&&yy1<0)flag=1;
165 {
166 if(X==0)
167 {
168 acy=0;
169 if(xxN%xx1)
170 {
171 flag=1;
172 }
173 else
174 {
175 LL ack=quick1(xx1,mod-2);
176 acx=xxN*ack%mod;
177 }
178 }
179 else
180 {
181
182 if(X%yy1)
183 {
184 flag=1;
185 }
186 else
187 { if(yy1>0&&X<0||yy1<0&&X>0)flag=1;
188 LL ack=quick1(yy1,mod-2);
189 acy=X/yy1%mod;
190 xxN-=ccy*acy;
191 if(xxN<0)flag=1;
192 if(xxN%xx1)
193 {
194 flag=1;
195 }
196 else
197 {
198 LL ack=quick1(xx1,mod-2);
199 acx=xxN/xx1;
200 }
201 }
202 }
203
204 }
205 printf("Case %d: ",s);
206 if(flag)
207 {
208 printf("Impossible\n");
209 }
210 else
211 {
212 maxtr cp;
213 Init(&cp);
214 maxtr akk=quick(cp,K-1);
215 LL xxx1=(akk.m[0][0]+akk.m[1][0])*acx%mod;
216 LL yyy1=(akk.m[0][1]+akk.m[1][1])*acy%mod;
217 printf("%lld\n",(xxx1+yyy1)%mod);
218 }
219 }
220 return 0;
221 }

1052 - String Growth的更多相关文章

  1. lightoj 1052 - String Growth & uva 12045 - Fun with Strings 矩阵

    思路:很容易发现规律,数列和Fib数列一样的. 记开始的时候啊a的个数为Y,b的个数为X.建立矩阵. 代码如下: #include<iostream> #include<cstdio ...

  2. 为什么operator>>(istream&, string&)能够安全地读入长度未知的字符串?

    一般而言,实现"读入用户输入的字符串",程序中自然不能对用户输入的长度有所限定.这在C++中很容易实现,而在C中确没那么容易. 这一疑问,我在刚学C++的时候也在脑中闪现过:不过很 ...

  3. hihocoder #1052 基因工程

    传送门:基因工程 这道题拖了好久,一直没有清晰的思路. 当然,$K\le\frac{N}{2}$时,比较简单.下面我着重讲一下当$K>\frac{N}{2}$,即前$K$个字符与后$K$个字符有 ...

  4. BZOJ 1052: [HAOI2007]覆盖问题

    BZOJ 1052: [HAOI2007]覆盖问题 题意:给定平面上横纵坐标在-1e9~1e9内的20000个整数点的坐标,用三个大小相同边平行于坐标轴的正方形覆盖(在边界上的也算),问正方形的边长最 ...

  5. FP—Growth算法

    FP_growth算法是韩家炜老师在2000年提出的关联分析算法,该算法和Apriori算法最大的不同有两点: 第一,不产生候选集,第二,只需要两次遍历数据库,大大提高了效率,用31646条测试记录, ...

  6. PAT 1052 卖个萌 (20)(代码+思路)

    1052 卖个萌 (20)(20 分) 萌萌哒表情符号通常由"手"."眼"."口"三个主要部分组成.简单起见,我们假设一个表情符号是按下列格 ...

  7. PAT——1052. 卖个萌

    萌萌哒表情符号通常由“手”.“眼”.“口”三个主要部分组成.简单起见,我们假设一个表情符号是按下列格式输出的: [左手]([左眼][口][右眼])[右手] 现给出可选用的符号集合,请你按用户的要求输出 ...

  8. 【BZOJ】1052: [HAOI2007]覆盖问题(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1052 首先膜拜题解orz,表示只能想到二分... 贪心就是每一次找到一个最小的能包围所有点的矩阵,然 ...

  9. Pat 1052 Linked List Sorting (25)

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

随机推荐

  1. LightningChart JS v.3.3.0全新版本现已发布!

    LightningChart JS v.3.3.0已经发布啦!!! 欢迎了解更多关于最新的性能改进.新的用户界面功能和新的图表类型的信息! WebGL兼容性和新的UI功能 WebGL是Lightnin ...

  2. 并发 并行 进程 线程 协程 异步I/O python async

    一些草率不精确的观点: 并发: 一起发生,occurence: sth that happens. 并行: 同时处理. parallel lines: 平行线.thread.join()之前是啥?落霞 ...

  3. Java偏向锁浅析

    偏向锁的定义 顾名思义,偏向锁会偏向第一个访问锁的线程. 如果在接下来的运行过程中,该锁没有被其他线程访问,这持有偏向锁的线程将永远不需要同步 如果在运行过程中,遇到了其他线程抢占锁,则持有偏向锁的线 ...

  4. day11 序列化组件、批量出入、自定义分页器

    day11 序列化组件.批量出入.自定义分页器 今日内容详细 ajax实现删除二次提醒(普通版本) ajax结合第三方插件sweetalert实现二次提醒(样式好看些) ajax如何发送文件数据 aj ...

  5. 【leetcode】952. Largest Component Size by Common Factor(Union find)

    You are given an integer array of unique positive integers nums. Consider the following graph: There ...

  6. 【Linux】【Services】【VersionControl】Git基础概念及使用

    1. 简介 1.1. 版本控制工具: 本地版本控制系统: 集中化版本控制系统:CVS,SVN 分布式版本控制系统: BitKeeper,Git 1.2. 官方网站: https://git-scm.c ...

  7. linux 配置本地yum

    1.挂载光盘 #挂载光盘 mount /dev/cdrom /mnt/cdrom 2.修改yum.conf, 运行 vi /etc/yum.conf,文件替换成如下内容 [main] cachedir ...

  8. Ribbon详解

    转自Ribbon详解 简介 ​ Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现.通过Spring Cloud的封装,可以让 ...

  9. windows下更换MySql数据库数据文件夹位置

    详细解决地址 ,感谢博主  :https://blog.csdn.net/u010953266/article/details/56499361 概述 由于更换硬盘,系统重新安装了一遍,原来的mysq ...

  10. 编译工具ant部署

    目录 一.环境准备 二.安装 三.使用验证 一.环境准备 当前环境:centos7.3一台 软件版本:ant-1.9 部署目录:/usr/local/ant yum依赖 yum -y java-1.8 ...