Count the Values of k

目录

题目总览

思路

参考代码

原题链接: CF1933C Turtle Fingers: Count the Values of k


题目总览

# Turtle Fingers: Count the Values of k

## 题面翻译

给你三个**正**整数 $a$ 、 $b$ 和 $l$ ( $a,b,l>0$ )。

可以证明,总有一种方法可以选择**非负**(即 $\ge 0$ )的整数 $k$ 、 $x$ 和 $y$ ,使得 $l = k \cdot a^x \cdot b^y$ .

你的任务是找出所有这些方法中 $k$ 的不同可能值的个数。

**输入**

第一行包含整数 $t$ ( $1 \le t \le 10^4$ ) 

接下来的 $t$ 行包含三个整数 $a$ 、 $b$ 和 $l$ ( $2 \le a, b \le 100$ 、 $1 \le l \le 10^6$ )

**注**

在第一个测试案例中, $a=2, b=5, l=20$ . $k$ (及相应的 $x,y$ )的可能值如下:

- 选择 $k = 1, x = 2, y = 1$ 。然后选择 $k \cdot a^x \cdot b^y = 1 \cdot 2^2 \cdot 5^1 = 20 = l$ 。
- 选择 $k = 2, x = 1, y = 1$ 。然后是 $k \cdot a^x \cdot b^y = 2 \cdot 2^1 \cdot 5^1 = 20 = l$ 。
- 选择 $k = 4, x = 0, y = 1$ 。然后选择 $k \cdot a^x \cdot b^y = 4 \cdot 2^0 \cdot 5^1 = 20 = l$ 。
- 选择 $k = 5, x = 2, y = 0$ 。然后选择 $k \cdot a^x \cdot b^y = 5 \cdot 2^2 \cdot 5^0 = 20 = l$ 。
- 选择 $k = 10, x = 1, y = 0$ 。然后选择 $k \cdot a^x \cdot b^y = 10 \cdot 2^1 \cdot 5^0 = 20 = l$ 。
- 选择 $k = 20, x = 0, y = 0$ 。然后选择 $k \cdot a^x \cdot b^y = 20 \cdot 2^0 \cdot 5^0 = 20 = l$ 。

在第二个测试案例中,选择 $a=2, b=5, l=21$ 。注意 $l = 21$ 不能被 $a = 2$ 或 $b = 5$ 整除。因此,我们只能设置 $x = 0, y = 0$ ,即对应于 $k = 21$ 。

第三个测试情形是 $a=4, b=6, l=48$ 。 $k$ 的可能值(以及相应的 $x,y$ )是(和对应的 $x,y$ 的可能值如下:

- 选择 $k = 2, x = 1, y = 1$ 。然后选择 $k \cdot a^x \cdot b^y = 2 \cdot 4^1 \cdot 6^1 = 48 = l$ 。
- 选择 $k = 3, x = 2, y = 0$ 。然后是 $k \cdot a^x \cdot b^y = 3 \cdot 4^2 \cdot 6^0 = 48 = l$ 。
- 选择 $k = 8, x = 0, y = 1$ 。然后选择 $k \cdot a^x \cdot b^y = 8 \cdot 4^0 \cdot 6^1 = 48 = l$ 。
- 选择 $k = 12, x = 1, y = 0$ 。然后选择 $k \cdot a^x \cdot b^y = 12 \cdot 4^1 \cdot 6^0 = 48 = l$ 。
- 选择 $k = 48, x = 0, y = 0$ 。然后选择 $k \cdot a^x \cdot b^y = 48 \cdot 4^0 \cdot 6^0 = 48 = l$ 。

## 题目描述

You are given three positive integers $ a $ , $ b $ and $ l $ ( $ a,b,l>0 $ ).

It can be shown that there always exists a way to choose non-negative (i.e. $ \ge 0 $ ) integers $ k $ , $ x $ , and $ y $ such that $ l = k \cdot a^x \cdot b^y $ .

Your task is to find the number of distinct possible values of $ k $ across all such ways.

## 输入格式

The first line contains the integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases.

The following $ t $ lines contain three integers, $ a $ , $ b $ and $ l $ ( $ 2 \le a, b \le 100 $ , $ 1 \le l \le 10^6 $ ) — description of a test case.

## 输出格式

Output $ t $ lines, with the $ i $ -th ( $ 1 \le i \le t $ ) line containing an integer, the answer to the $ i $ -th test case.

## 样例 #1

### 样例输入 #1

```

11
2 5 20
2 5 21
4 6 48
2 3 72
3 5 75
2 2 1024
3 7 83349
100 100 1000000
7 3 2
2 6 6
17 3 632043


```

### 样例输出 #1

```

6
1
5
12
6
11
24
4
1
3
24


```

## 提示

In the first test case, $ a=2, b=5, l=20 $ . The possible values of $ k $ (and corresponding $ x,y $ ) are as follows:

- Choose $ k = 1, x = 2, y = 1 $ . Then $ k \cdot a^x \cdot b^y = 1 \cdot 2^2 \cdot 5^1 = 20 = l $ .
- Choose $ k = 2, x = 1, y = 1 $ . Then $ k \cdot a^x \cdot b^y = 2 \cdot 2^1 \cdot 5^1 = 20 = l $ .
- Choose $ k = 4, x = 0, y = 1 $ . Then $ k \cdot a^x \cdot b^y = 4 \cdot 2^0 \cdot 5^1 = 20 = l $ .
- Choose $ k = 5, x = 2, y = 0 $ . Then $ k \cdot a^x \cdot b^y = 5 \cdot 2^2 \cdot 5^0 = 20 = l $ .
- Choose $ k = 10, x = 1, y = 0 $ . Then $ k \cdot a^x \cdot b^y = 10 \cdot 2^1 \cdot 5^0 = 20 = l $ .
- Choose $ k = 20, x = 0, y = 0 $ . Then $ k \cdot a^x \cdot b^y = 20 \cdot 2^0 \cdot 5^0 = 20 = l $ .

In the second test case, $ a=2, b=5, l=21 $ . Note that $ l = 21 $ is not divisible by either $ a = 2 $ or $ b = 5 $ . Therefore, we can only set $ x = 0, y = 0 $ , which corresponds to $ k = 21 $ .

In the third test case, $ a=4, b=6, l=48 $ . The possible values of $ k $ (and corresponding $ x,y $ ) are as follows:

- Choose $ k = 2, x = 1, y = 1 $ . Then $ k \cdot a^x \cdot b^y = 2 \cdot 4^1 \cdot 6^1 = 48 = l $ .
- Choose $ k = 3, x = 2, y = 0 $ . Then $ k \cdot a^x \cdot b^y = 3 \cdot 4^2 \cdot 6^0 = 48 = l $ .
- Choose $ k = 8, x = 0, y = 1 $ . Then $ k \cdot a^x \cdot b^y = 8 \cdot 4^0 \cdot 6^1 = 48 = l $ .
- Choose $ k = 12, x = 1, y = 0 $ . Then $ k \cdot a^x \cdot b^y = 12 \cdot 4^1 \cdot 6^0 = 48 = l $ .
- Choose $ k = 48, x = 0, y = 0 $ . Then $ k \cdot a^x \cdot b^y = 48 \cdot 4^0 \cdot 6^0 = 48 = l $ .

思路

这道题 a 和 b 都很小,而 l 却很大。

所以枚举 x 和 y,计算 k 就可以了。

参考代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;

//const int N = ;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int t;
	cin>>t;
	
	while(t--)
	{
		set <ll> ans;
		ll a, b, l;
		
		cin>>a>>b>>l;
		
		for(ll i = 1, a_x = 1; a_x <= l; i++, a_x *= a)
		{
			for(ll j = 1, b_y = 1; b_y <= l; j++, b_y *= b)
			{
				ll k = l / (a_x * b_y);
				
				if(k * a_x * b_y == l)
					ans.insert(k);
			}	
		}
		
		cout<<ans.size()<<"\n";
	}
	
	return 0;
}

原题链接:CF1933C Turtle Fingers: Count the Values of k


喜欢文章的可以点个关注+收藏,或者顺手一个小心心也可以,最近刷题较多,博文发的很频繁,制作不易,不喜勿喷(全文约 3.4K 字不值得你你的关注吗?)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学废c++

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值