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

相关推荐
dragoooon3410 分钟前
[hot100 NO.13~18]
算法
郭涤生10 分钟前
大白话Proactor模式
linux·网络·c++
WangLanguager11 分钟前
Prototypical Networks 在图像识别中表现如何?
算法
我是你们的明哥13 分钟前
A*(A-Star)算法详解:智能路径规划的核心技术
后端·算法
我是你们的明哥19 分钟前
从 N 个商品中找出总价最小的 K 个方案
后端·算法
郭涤生26 分钟前
大白话Reactor模式
linux·c++
民乐团扒谱机27 分钟前
【微实验】谱聚类之大规模数据应用——Nyström 方法
人工智能·算法·机器学习·matlab·数据挖掘·聚类·谱聚类
CoderYanger28 分钟前
A.每日一题——3606. 优惠券校验器
java·开发语言·数据结构·算法·leetcode
CoderYanger39 分钟前
D.二分查找-基础——744. 寻找比目标字母大的最小字母
java·开发语言·数据结构·算法·leetcode·职场和发展
元亓亓亓42 分钟前
LeetCode热题100--347. 前 K 个高频元素--中等
数据结构·算法·leetcode