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

相关推荐
董董灿是个攻城狮3 小时前
AI视觉连载8:传统 CV 之边缘检测
算法
blasit10 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
AI软著研究员10 小时前
程序员必看:软著不是“面子工程”,是代码的“法律保险”
算法
FunnySaltyFish11 小时前
什么?Compose 把 GapBuffer 换成了 LinkBuffer?
算法·kotlin·android jetpack
颜酱12 小时前
理解二叉树最近公共祖先(LCA):从基础到变种解析
javascript·后端·算法
地平线开发者1 天前
SparseDrive 模型导出与性能优化实战
算法·自动驾驶
董董灿是个攻城狮1 天前
大模型连载2:初步认识 tokenizer 的过程
算法
地平线开发者1 天前
地平线 VP 接口工程实践(一):hbVPRoiResize 接口功能、使用约束与典型问题总结
算法·自动驾驶
罗西的思考1 天前
AI Agent框架探秘:拆解 OpenHands(10)--- Runtime
人工智能·算法·机器学习
HXhlx1 天前
CART决策树基本原理
算法·机器学习