codeforces 612A The Text Splitting(扩展欧几里得)

A. The Text Splitting

You are given the string s of length n and the numbers p, q. Split the string s to pieces of length p and q.

For example, the string "Hello" for p = 2, q = 3 can be split to the two strings "Hel" and "lo" or to the two strings "He" and "llo".

Note it is allowed to split the string s to the strings only of length p or to the strings only of length q (see the second sample test).

Input

The first line contains three positive integers n, p, q (1 ≤ p, q ≤ n ≤ 100).

The second line contains the string s consists of lowercase and uppercase latin letters and digits.

Output

If it‘s impossible to split the string s to the strings of length p and q print the only number "-1".

Otherwise in the first line print integer k — the number of strings in partition of s.

Each of the next k lines should contain the strings in partition. Each string should be of the length p or q. The string should be in order of their appearing in string s — from left to right.

If there are several solutions print any of them.

Sample test(s)

input

5 2 3Hello

output

2Hello

input

10 9 5Codeforces

output

2Codeforces

input

6 4 5Privet

output

-1

input

8 1 1abacabac

output

8abacabac
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 void exgcd(int a,int b,int& d,int& x,int& y)
 6 {
 7     if(!b)d=a,x=1,y=0;
 8     else exgcd(b,a%b,d,y,x),y-=x*(a/b);
 9 }
10 int main()
11 {
12     char s[105];
13     int n,p,q,d,x,y;
14     bool ok;
15     scanf("%d%d%d",&n,&p,&q);
16     scanf("%s",s);
17     int t=0;
18     if(n%p==0)
19     {
20         n/=p;
21         printf("%d\n",n);
22         for(int i=0;i<n;i++)
23         {
24             for(int j=0;j<p;j++)
25                 printf("%c",s[t++]);
26             puts("");
27         }
28     }
29     else if(n%q==0)
30     {
31         n/=q;
32         printf("%d\n",n);
33         for(int i=0;i<n;i++)
34         {
35             for(int j=0;j<q;j++)
36                 printf("%c",s[t++]);
37             puts("");
38         }
39     }
40     else
41     {
42         ok=1;
43         exgcd(p,q,d,x,y);
44         if(n%d!=0)
45         {
46             puts("-1");
47             return 0;
48         }
49         else
50         {
51             x*=n/d,y*=n/d;
52             int a=p/d,b=q/d;
53             int tx=x,ty=y;
54             while(x<0||y<0)
55             {
56                 x+=b;
57                 y-=a;
58                 if(tx*x<0&&ty*y<0)
59                 {
60                     while(x<0||y<0)
61                     {
62                         x-=b;
63                         y+=a;
64                         if(tx*x<0&&ty*y<0)
65                         {
66                             ok=0;
67                             break;
68                         }
69                     }
70                     break;
71                 }
72             }
73         }
74         if(ok)
75         {
76             printf("%d\n",x+y);
77             for(int i=0;i<x;i++)
78             {
79                 for(int j=0;j<p;j++)
80                     printf("%c",s[t++]);
81                 puts("");
82             }
83             for(int i=0;i<y;i++)
84             {
85                 for(int j=0;j<q;j++)
86                     printf("%c",s[t++]);
87                 puts("");
88             }
89         }
90         else
91             puts("-1");
92     }
93     return 0;
94 }
时间: 2024-07-30 10:19:10

codeforces 612A The Text Splitting(扩展欧几里得)的相关文章

[codeforces 200 E Tractor College]枚举,扩展欧几里得,三分

题目出自 Codeforces Round #126 (Div. 2) 的E. 题意大致如下:给定a,b,c,s,求三个非负整数x,y,z,满足0<=x<=y<=z,ax+by+cz=s,使得f(x,y,z)=|ax-by|+|by-cz|最小 思路:枚举z,得到一个方程ax+by=s-cz,用扩展欧几里得求出这个方程的一个解,然后三分通解的整系数,求出最小f值.至于为什么可以三分画画图就清楚了,两个绝对值函数叠加在一起最多只有三种状态(第一维表示临界点较小的那个绝对值函数):(降,降)

【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions

题目链接: http://codeforces.com/problemset/problem/710/D 题目大意: 两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个. 0 < a1, a2 ≤ 2·109,  - 2·109 ≤ b1, b2, L, R ≤ 2·109, L ≤ R). 题目思路: [数论][扩展欧几里得] 据题意可得同余方程组 x=b1(mod a1) 即 x=k1*a1+b1 x=b2(mod a2) x=k2*a2+b2 化简,k1*a1=k2*a2

UVa 11768 格点判定(扩展欧几里得求线段整点)

https://vjudge.net/problem/UVA-11768 题意: 给定两个点A(x1,y1)和B(x2,y2),均为0.1的整数倍.统计选段AB穿过多少个整点. 思路: 做了这道题之后对于扩展欧几里得有了全面的了解. 根据两点式公式求出直线 ,那么ax+by=c 中的a.b.c都可以确定下来了. 接下来首先去计算出一组解(x0,y0),因为根据这一组解,你可以写出它的任意解,其中,K取任何整数. 需要注意的是,这个 a' 和 b' 是很重要的,比如说 b' ,它代表的是x每隔 b

【扩展欧几里得】BZOJ1477-青蛙的约会

一直在WA,后来我发现我把东西看反了-- [题目大意] 给出一个长度为L的环状坐标轴,两个点开始时位于(X,0).(Y,0).每次两点分别往右边移动m和n,问能否相遇? [思路] 由题意,可得: X+mt=Y+nt(mod L) (X+mt)-(Y+nt)=L*k (n-m)t+L*k=X-Y. 可以用扩展欧几里得来做.具体来说,显然要满足n-m和L的最大公约数(记为d)要整除X-Y,否则无解.这个可以在扩展欧几里得中求出. 式子可以化简为:[(n-m)/d]*t+(L/d)*k=(X-Y)/d

POJ 1061 青蛙的约会 扩展欧几里得

扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; ll exgcd(ll a, ll b, ll&x, ll&y) { if (b ==

POJ 2115 (模线性方程 -&gt; 扩展欧几里得)

题意: for(i=A ; i!=B ;i +=C)循环语句,问在k位操作系统中循环结束次数. 若在有则输出循环次数. 否则输出死循环. 存在这样的情况:i= 65533 :i<=2:i+= 4:时i = 2: 由模线性方程->扩展欧几里得 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> using

POJ 2115 C Looooops(扩展欧几里得应用)

题目地址:POJ 2115 水题..公式很好推.最直接的公式就是a+n*c==b+m*2^k.然后可以变形为模线性方程的样子,就是 n*c+m*2^k==b-a.即求n*c==(b-a)mod(2^k)的最小解.(真搞不懂为什么训练的时候好多人把青蛙的约会都给做出来了,这题却一直做不出来.....这两道不都是推公式然后变形吗.....) 代码如下: #include <iostream> #include <cstdio> #include <string> #incl

【64测试20161112】【Catalan数】【数论】【扩展欧几里得】【逆】

Problem: n个人(偶数)排队,排两行,每一行的身高依次递增,且第二行的人的身高大于对应的第一行的人,问有多少种方案.mod 1e9+9 Solution: 这道题由1,2,5,14 应该想到Catalan数,但是我却花了两个小时去找递推式. 首先 Catalan数 : 基本规律:1,2,5,14,42,132,.......... 典型例题: 1.多边形分割.一个多边形分为若干个三角形有多少种分法. C(n)=∑(i=2...n-1)C(i)*C(n-i+1) 2.排队问题:转化为n个人

HDU1576(扩展欧几里得)

A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4252    Accepted Submission(s): 3277 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Input 数据的第一行是一个T