Codeforces Round 173 B. Digits(2043)

判断一个数字的N!个,是否可以被奇数整除。

原题描述

https://codeforces.com/contest/2043/problem/B

Educational Codeforces Round 173 (Rated for Div. 2)

B.Digits

Artem wrote the digit d on the board exactly n! (for example:5!=1*2*3*4*5 ) times in a row.

So,he got the number dddd....ddd(exactly n! digits)

Now he is curious about which odd digits form 1 to 9 divide the number written on the board.

(odd:1,3,5,7,9 in(1 to 9 % 2!=0),

even:2,4,8,10(1 to 10) %2==0)

Input

The first line contains a single integer t(1<=t<=100)-the numbers of test cases.

The next t tests cases follow.

Each test case consists of a sigle line containing two integers n and d((2≤n≤109, 1≤d≤9).

Output

For each test case,output the odd digits in ascending order that divide the number written on the board.

Example

Input

3

2 6

7 1

8 5

OutputCopy

1 3

1 3 7 9

1 3 5 7 9

Note

The factorial of a positive integer n(n!) is the product of all integers from 1 to n.

For example, the factorial of 5 is 1*2*3*4*5=120

思路:

首先需要知道的知识

1.N阶层=N!=1*2*3*....N,例如5! is 1*2*3*4*5=120

2 多个1,2,3.....9 整除的情况。

(1)3!=6个1,2,3....9可以整除3

(2)3!=6个1,2,3....9可以整除7

(3)3!=6个1,2,3....9 并且整除3,9的也可以

(4)6!的数字1,2,3...9 可以整除9

3 奇数odd=整除2有余数

根据以上规则来编写

STEP1 获得10以内的odd奇数,放在列表里list

STEP2 判断是否%1,3,5,7,9 每一种情况,一共5个情况,分别输出即可。

当1的时候,整除1==0即可

当3的时候,整除3==0 或者3!=6个单数,也是可以整除3,7的,这里就输出这个整除数

当5,仅仅1个

当7,类似3

当9,需要注意:整除9,6!也可以,还有3!并且%3 也是可以。

cpp 复制代码
/*
https://codeforces.com/contest/2043/problem/B
Educational Codeforces Round 173 (Rated for Div. 2)
B.Digits

Artem wrote the digit d on the board exactly n! (for example:5!=1*2*3*4*5 ) times in a row.
So,he got the number dddd....ddd(exactly n! digits)

Now he is curious about which odd digits form 1 to 9 divide the number written on the board.
(odd:1,3,5,7,9 in(1 to 9 % 2!=0),
even:2,4,8,10(1 to 10) %2==0)

Input
The first line contains a single integer t(1<=t<=100)-the numbers of test cases.
The next t tests cases follow.

Each test case consists of a sigle line containing two integers n and d((2≤n≤109, 1≤d≤9).
Output
For each test case,output the odd digits in ascending order that divide the number written on the board.

Example
Input
3
2 6
7 1
8 5
OutputCopy
1 3
1 3 7 9
1 3 5 7 9

Note
The factorial of a positive integer n(n!) is the product of all integers from 1 to n.
For example, the factorial of 5 is 1*2*3*4*5=120
*/


#include <iostream>
#include <string>
using namespace std;

//ref others
void d_divide_odd()
{
	int n = 0, d = 0;
	cin >> n >> d;
	char space1 = ' ';
	int index = 0;
	int *list = new int[5];

	//get odd : 1,3,5,7,9
	for (int i = 1; i <=9; i = i + 2)
	{
		list[index] = i;
		index++;
	}
	
	//show
	//1 in(1,2,3,4,5,6,7,8,9)
	if (d % 1==0)
	{
		cout << list[0] << space1; 
	}

	// case : 3
	//1) for:1..9 has odd (3,6,9)  %3 == 0	
	//2) new math knowledge: the factorial of n! = 3!=1*2*3= 6 times of d = %3 == 0
	if (d%3 == 0 || n>=3) 
	{   //3!=1*2*3=6
		cout << list[1] << space1;
	}

	// case : 5
	//only one 5 in(1,2,3,4,5,6,7,8,9)
	if (d % 5 == 0)
	{
		cout << list[2] << space1;
	}

	// case : 7
	//1) only one 7 in(1,2,3,4,5,6,7,8,9)
	//2) new math knowledge: the factorial of n! = 3!=1*2*3= 6 times of d = %7 == 0
	if (d % 7 == 0 || n >= 3)
	{  
		cout << list[3] << space1;
	}
	//Note: you can try to make 1,2,3...9 of n!(1,2,3)times % ==0

	// case : 9
	// 9 is 3,6,9
	if (d % 9==0 || n >=6 || (d%3 == 0 && n >= 3) )
	{
		cout << list[4] << space1;
	}
	//Note: you can try to make 1,2,3...9 of n!(1,2,3,4,5,6)times % 9 ==0
	cout << endl;
}

int main()
{
	int t = 0;
	cin >> t;
	while (t--)
	{
		d_divide_odd();
	}
	return 0;
}

完成,这里需要学习的新知识就是,3!%3,7==0,另外%3 并且N>=3!也是可以整除9

相关推荐
m0_743106464 小时前
LOBE-GS:分块&致密化效率提升
人工智能·算法·计算机视觉·3d·几何学
徐子童4 小时前
优选算法---字符串
java·算法·字符串·笔试·高精度相乘
西瓜啵啵奶茶4 小时前
LeetCode 热题 100 : 普通数组
算法
jikiecui4 小时前
信奥崔老师:C++ 程序设计入门
算法
Q741_1474 小时前
C++ 位运算 高频面试考点 力扣 面试题 17.19. 消失的两个数字 题解 每日一题
c++·算法·leetcode·面试·位运算
Jacob00004 小时前
[Decision Tree] H(D) & IG & IGR
算法·面试
vadvascascass4 小时前
平滑加权轮询负载均衡的底层逻辑
java·算法·负载均衡
初圣魔门首席弟子4 小时前
C++ STL string(字符串)学习笔记
c++·笔记·学习
CoovallyAIHub4 小时前
Transformer作者开源进化计算新框架,样本效率暴增数十倍!
深度学习·算法·计算机视觉