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

相关推荐
AAA简单玩转程序设计11 分钟前
C++进阶小技巧:让代码从"能用"变"优雅"
前端·c++
vir0228 分钟前
密码脱落(最长回文子序列)
数据结构·c++·算法
福尔摩斯张41 分钟前
二维数组详解:定义、初始化与实战
linux·开发语言·数据结构·c++·算法·排序算法
大佬,救命!!!1 小时前
C++函数式策略模式代码练习
开发语言·c++·学习笔记·学习方法·策略模式·迭代加深·多文件编译
冰西瓜6001 小时前
模与内积(五)矩阵分析与应用 国科大
线性代数·算法·矩阵
努力学算法的蒟蒻1 小时前
day17(11.18)——leetcode面试经典150
算法·leetcode·面试
缘友一世1 小时前
模型微调DPO算法原理深入学习和理解
算法·模型微调·dpo
未若君雅裁1 小时前
斐波那契数列 - 动态规划实现 详解笔记
java·数据结构·笔记·算法·动态规划·代理模式
断剑zou天涯1 小时前
【算法笔记】从暴力递归到动态规划(三)
java·算法·动态规划
RQ_ghylls1 小时前
2.excel每3行计算一个均值,将高于均值的单元格设置背景红色
算法·均值算法·word·excel