Divisibility Rules CodeForces - 180B 神奇代码解读

B. Divisibility Rules
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya studies divisibility rules at school. Here are some of them:

  • Divisibility by 2. A number is divisible by 2 if and only if its last digit is divisible by 2 or in other words, is even.
  • Divisibility by 3. A number is divisible by 3 if and only if the sum of its digits is divisible by 3.
  • Divisibility by 4. A number is divisible by 4 if and only if its last two digits form a number that is divisible by 4.
  • Divisibility by 5. A number is divisible by 5 if and only if its last digit equals 5 or 0.
  • Divisibility by 6. A number is divisible by 6 if and only if it is divisible by 2 and 3 simultaneously (that is, if the last digit is even and the sum of all digits is divisible by 3).
  • Divisibility by 7. Vasya doesn't know such divisibility rule.
  • Divisibility by 8. A number is divisible by 8 if and only if its last three digits form a number that is divisible by 8.
  • Divisibility by 9. A number is divisible by 9 if and only if the sum of its digits is divisible by 9.
  • Divisibility by 10. A number is divisible by 10 if and only if its last digit is a zero.
  • Divisibility by 11. A number is divisible by 11 if and only if the sum of digits on its odd positions either equals to the sum of digits on the even positions, or they differ in a number that is divisible by 11.

Vasya got interested by the fact that some divisibility rules resemble each other. In fact, to check a number's divisibility by 2458 and 10it is enough to check fulfiling some condition for one or several last digits. Vasya calls such rules the 2-type rules.

If checking divisibility means finding a sum of digits and checking whether the sum is divisible by the given number, then Vasya calls this rule the 3-type rule (because it works for numbers 3 and 9).

If we need to find the difference between the sum of digits on odd and even positions and check whether the difference is divisible by the given divisor, this rule is called the 11-type rule (it works for number 11).

In some cases we should divide the divisor into several factors and check whether rules of different types (2-type, 3-type or 11-type) work there. For example, for number 6 we check 2-type and 3-type rules, for number 66 we check all three types. Such mixed divisibility rules are called 6-type rules.

And finally, there are some numbers for which no rule works: neither 2-type, nor 3-type, nor 11-type, nor 6-type. The least such number is number 7, so we'll say that in such cases the mysterious 7-type rule works, the one that Vasya hasn't discovered yet.

Vasya's dream is finding divisibility rules for all possible numbers. He isn't going to stop on the decimal numbers only. As there are quite many numbers, ha can't do it all by himself. Vasya asked you to write a program that determines the divisibility rule type in the b-based notation for the given divisor d.

Input

The first input line contains two integers b and d (2 ≤ b, d ≤ 100) — the notation system base and the divisor. Both numbers are given in the decimal notation.

Output

On the first output line print the type of the rule in the b-based notation system, where the divisor is d: "2-type", "3-type", "11-type", "6-type" or "7-type". If there are several such types, print the one that goes earlier in the given sequence. If a number belongs to the 2-type, print on the second line the least number of the last b-based digits that we will need to use to check the divisibility.

Examples
input
Copy
10 10
output
2-type
1
input
Copy
2 3
output
11-type
Note

The divisibility rule for number 3 in binary notation looks as follows: "A number is divisible by 3 if and only if the sum of its digits that occupy the even places differs from the sum of digits that occupy the odd places, in a number that is divisible by 3". That's an 11-type rule. For example, 2110 = 101012. For it the sum of digits on odd positions equals 1 + 1 + 1 = 3, an on even positions — 0 + 0 = 0. The rule works and the number is divisible by 3.

In some notations a number can fit into the 3-type rule and the 11-type rule. In this case the correct answer is "3-type".


神奇代码:

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;

int b,d,is[9];

int gcd(int a,int b)
{
	return !b?a:gcd(b,a%b);
}

int main()
{
	scanf("%d%d",&b,&d);
	int k=0;
	for(;gcd(b,d)>1;k++) d/=gcd(b,d);
	if(d==1)//将2-type排除
	{
		printf("2-type\n%d",k);
	}
	else if(k==0 && b%d==1)//3-type
	{
		printf("3-type");
	}
	else if(k==0 && b%d==d-1)//11-type
	{
		printf("11-type");
	}
	else if((b*b-1)/(b%2+1)%d==0)//最神奇的地方,如果符合3-type或7-type,那么一定满足2-type
	{
		printf("6-type");//b*b-1=(b-1)*(b+1),当b为偶数时,(b%2+1)=1,(b-1)和(b+1)互质,所以可以直接mod d,b为奇数时,除一个二让他们互质
	}
	else printf("7-type");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值