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

相关推荐
liuyao_xianhui14 分钟前
优选算法_最小基因变化_bfs_C++
java·开发语言·数据结构·c++·算法·哈希算法·宽度优先
黎阳之光31 分钟前
数智技术如何赋能空天地一体化,领跑低空经济新赛道
大数据·人工智能·算法·安全·数字孪生
cch891837 分钟前
易语言与C++:编程语言终极对决
开发语言·c++
HABuo40 分钟前
【linux线程(三)】生产者消费者模型(条件变量阻塞队列版本、信号量环形队列版本)详细剖析
linux·运维·服务器·c语言·c++·ubuntu·centos
小肝一下1 小时前
每日两道力扣,day2
c++·算法·leetcode·职场和发展
漂流瓶jz1 小时前
UVA-11846 找座位 题解答案代码 算法竞赛入门经典第二版
数据结构·算法·排序算法·深度优先·aoapc·算法竞赛入门经典·uva
米粒12 小时前
力扣算法刷题 Day 31 (贪心总结)
算法·leetcode·职场和发展
少许极端2 小时前
算法奇妙屋(四十)-贪心算法学习之路7
java·学习·算法·贪心算法
AlenTech2 小时前
647. 回文子串 - 力扣(LeetCode)
算法·leetcode·职场和发展
py有趣2 小时前
力扣热门100题之合并两个有序链表
算法·leetcode·链表