编程介的小学生 2016-12-23 14:00 采纳率: 20.5%
浏览 781
已采纳

How Many Free Dimensions?

Description

In calculating the outer product of vectors, existence of constraints usually makes it that not all the components of the product are independent of the others. For example, the outer product of two two-dimensional vectors A = (a1, a2) and B = (b1, b2), C = A × B = (c11, c12, c21, c22). If we set c12 = c21, then only three of the four components of C is independent, or C has three free dimensions.

Now we have k n-dimensional vectors A1, A2, …, Ak. The product of them is a vector of nk components Z = (zα) where α enumerates all possible subscripts i1i2…ik (1 ≤ ij ≤ n for all j s.t. 1 ≤ j ≤ k). We can then apply a rule of constraint of the form s1s2…sk=t1t2…tk to the subscripts. Here s1s2…sk and t1t2…tk are two strings consisting of the same set of lowercase letters. A letter appearing in one string will also appear in the other one and it can have multiple occurrences in a string. When a rule is applied, the letters in it are replaced by arbitrary integers between 1 and n (inclusive) provided that the same letters are replaced by the same integers and different letters are replaced by different integers. If a resulted string after replacements is p1p2…pk=q1q2…qk, let α be p1p2…pk and β be q1q2…qk, then we set zα = zβ. Given the number of vectors and their dimensions and a rule of constraint, you are required to compute the number of free dimensions of the product of the vectors.

Input

The input contains several test cases. Each test case consists of two lines followed by a blank one. On the first line there are two integers which are n and k in the order they appear. On the second line is a rule of constraint. Two zeroes on a separate line follow the last test case.

Output

For each test case, output one line containing the number of free dimensions of the product of vectors.

Sample Input

2 2
ij=ji

3 3
iij=jii

0 0
Sample Output

3
21

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-12-23 15:52
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误