cf 983A Finite or not?

一 原题

A. Finite or not?
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given several queries. Each query consists of three integers ppqq and bb. You need to answer whether the result of p/qp/q in notation with base bb is a finite fraction.

A fraction in notation with base bb is finite if it contains finite number of numerals after the decimal point. It is also possible that a fraction has zero numerals after the decimal point.

Input

The first line contains a single integer nn (1n1051≤n≤105) — the number of queries.

Next nn lines contain queries, one per line. Each line contains three integers ppqq, and bb (0p10180≤p≤10181q10181≤q≤10182b10182≤b≤1018). All numbers are given in notation with base 1010.

Output

For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.

Examples
input
Copy
2
6 12 10
4 3 10
output
Copy
Finite
Infinite
input
Copy
4
1 1 2
9 36 2
4 12 3
3 5 4
output
Copy
Finite
Finite
Finite
Infinite
Note

612=12=0,510612=12=0,510

43=1,(3)1043=1,(3)10

936=14=0,012936=14=0,012

412=13=0,13


二 分析

题意是说给你三个数p,q,b.问你分数p/q在b进制下是否是有穷小数。首先把q除去p,q的最大公约数得到新的q.p/q是有穷小数等价于新的q的素因子都是b的素因子。因为p,q,b最大都是max=10^18,直接分解素因子需要sqrt(max)次操作,会超时。我在比赛的时候想到的办法是求q和b的最大公约数t,只要分解q/t和t中较小的一个就可以了,复杂度就变成了两次开根。更多人选择的解法见下面代码,话说c++17已经自带了std::gcd函数,在头文件<numeric>里。

三 代码

#include <cstdio>
#include <numeric>

typedef long long LL;

using std::gcd;

int n;
LL p, q, b;

int main() {
	scanf("%d", &n);
	while (n--) {
		scanf("%lld%lld%lld", &p, &q, &b);
		q /= gcd(p, q);
		while (true) {
			LL g = gcd(q, b);
			if (g == 1) break;
			while (q % g == 0) q /= g;
		}
		if (q == 1) puts("Finite");
		else puts("Infinite");
	}
	return 0;
}

F. Attraction Theory time limit per test2 seconds memory limit per test256 megabytes Taylor Swift - Love Story (Taylor's Version) ⠀ There are 𝑛 people in positions 𝑝1,𝑝2,…,𝑝𝑛 on a one-dimensional coordinate axis. Initially, 𝑝𝑖=𝑖 for each 1≤𝑖≤𝑛. You can introduce an attraction at some integer coordinate 𝑥 (1≤𝑥≤𝑛), and then all the people will move closer to the attraction to look at it. Formally, if you put an attraction in position 𝑥 (1≤𝑥≤𝑛), the following changes happen for each person 𝑖 (1≤𝑖≤𝑛): if 𝑝𝑖=𝑥, no change; if 𝑝𝑖<𝑥, the person moves in the positive direction, and 𝑝𝑖 is incremented by 1; if 𝑝𝑖>𝑥, the person moves in the negative direction, and 𝑝𝑖 is decremented by 1. You can put attractions any finite number of times, and in any order you want. It can be proven that all positions of a person always stays within the range [1,𝑛], i.e. 1≤𝑝𝑖≤𝑛 at all times. Each position 𝑥 (1≤𝑥≤𝑛), has a value 𝑎𝑥 associated with it. The score of a position array [𝑝1,𝑝2,…,𝑝𝑛], denoted by 𝑠𝑐𝑜𝑟𝑒(𝑝), is ∑𝑛𝑖=1𝑎𝑝𝑖, i.e. your score increases by 𝑎𝑥 for every person standing at 𝑥 in the end. Over all possible distinct position arrays 𝑝 that are possible with placing attractions, find the sum of 𝑠𝑐𝑜𝑟𝑒(𝑝). Since the answer may be large, print it modulo 998244353. Input Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤104). The description of the test cases follows. The first line of each test case contains a single integer 𝑛 (1≤𝑛≤2⋅105). The second line of each test case contains 𝑛 integers — 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109) It is guaranteed that the sum of 𝑛 over all test cases does not exceed 2⋅105. Output For each test case, output a single line containing an integer: the sum of 𝑠𝑐𝑜𝑟𝑒(𝑝) over all possible distinct position arrays 𝑝 that are possible with placing attractions, modulo 998244353. Example inputCopy 7 1 1 2 5 10 3 1 1 1 4 1 1 1 1 4 10 2 9 7 5 1000000000 1000000000 1000000000 1000000000 1000000000 8 100 2 34 59 34 27 5 6 outputCopy 1 45 24 72 480 333572930 69365 Note In the first test case, the only possible result is that person 1 stays at 1. The score of that is 𝑎1=1. In the second test case, the following position arrays [𝑝1,𝑝2] are possible: [1,2], score 15; [1,1], score 10; [2,2], score 20. The sum of scores is 15+10+20=45. In the third test case, the following position arrays [𝑝1,𝑝2,𝑝3] are possible: [1,1,1]; [1,1,2]; [1,2,2]; [1,2,3]; [2,2,2]; [2,2,3]; [2,3,3]; [3,3,3]. Each has a score of 3, and thus the total sum is 24.解决这道题,用c++
09-25
<think> 问题:在Earth Engine中,当尝试使用`lon.isFinite()`时出现错误“lon.isFinite is not a function”。这通常是因为`lon`变量不是一个`ee.Number`对象,或者该对象没有`isFinite`方法。 原因分析: 1. 可能`ee.Number.parse`返回的不是一个`ee.Number`对象,特别是在解析失败时。 2. 或者,`feature.get('lon')`返回的值可能是`null`或者不是字符串,导致解析失败。 解决方案: 1. 使用更安全的类型检查方法。 2. 使用`ee.Algorithms.IsFinite`来检查数字是否有限。 修改后的代码部分: ```javascript var createPoint = function(feature) { // 显式转换为数字 var lon = ee.Number.parse(feature.get('lon')); var lat = ee.Number.parse(feature.get('lat')); // 验证数字有效性:使用ee.Algorithms.IsFinite var isLonFinite = ee.Algorithms.IsFinite(lon); var isLatFinite = ee.Algorithms.IsFinite(lat); var isValid = isLonFinite.and(isLatFinite); // 创建点(仅当数字有效时) return ee.Algorithms.If(isValid, ee.Feature(ee.Geometry.Point([lon, lat])), null ); }; ``` 但是,注意`ee.Number.parse`在解析非数字字符串时会返回`null`,而`ee.Algorithms.IsFinite`对于`null`会返回`false`。所以这样是可行的。 另一种更健壮的方法是先检查属性是否存在,然后使用`ee.Number`来转换(如果确定是数字类型,但之前可能被解析为字符串),或者先转换为字符串再解析。 考虑到错误,我们还可以先检查属性值是否为`ee.Number`类型,如果不是,则尝试转换。但通常,从CSV中读取的属性都是字符串。 因此,我们可以这样修改: ```javascript var createPoint = function(feature) { // 获取属性值 var lonValue = feature.get('lon'); var latValue = feature.get('lat'); // 将属性值转换为ee.Number(如果已经是数字则不变,如果是字符串则解析) var lon = ee.Number(lonValue); // 直接尝试转换为ee.Number,如果失败则返回invalid var lat = ee.Number(latValue); // 使用ee.Algorithms.IsFinite检查 var isLonFinite = ee.Algorithms.IsFinite(lon); var isLatFinite = ee.Algorithms.IsFinite(lat); var isValid = isLonFinite.and(isLatFinite); // 创建点(仅当数字有效时) return ee.Algorithms.If(isValid, ee.Feature(ee.Geometry.Point([lon, lat])), null ); }; ``` 注意:`ee.Number(value)`如果`value`是字符串,它会尝试解析字符串为数字。如果解析失败,会返回一个无效的数字(相当于NaN),而`ee.Algorithms.IsFinite`对于NaN会返回false。 因此,这个版本更简洁,并且可以处理字符串和数字类型的属性。 但是,如果属性值不是数字也不是数字字符串,那么`ee.Number`转换会得到一个无效数字,然后被`IsFinite`检测为false,从而被过滤。 所以,我们使用这个版本来修复错误。 完整修复后的代码: ```javascript // 加载CSV资产 var points = ee.FeatureCollection('projects/ee-heyunqi98/assets/sampled_data_1'); // 创建点几何(带类型转换和错误处理) var createPoint = function(feature) { // 尝试获取经纬度并转换为ee.Number var lon = ee.Number(feature.get('lon')); var lat = ee.Number(feature.get('lat')); // 验证数字有效性 var isLonFinite = ee.Algorithms.IsFinite(lon); var isLatFinite = ee.Algorithms.IsFinite(lat); var isValid = isLonFinite.and(isLatFinite); // 创建点(仅当数字有效时) return ee.Algorithms.If(isValid, ee.Feature(ee.Geometry.Point([lon, lat])), null ); }; // 应用函数并过滤无效点 var pointFeatures = points.map(createPoint).filter(ee.Filter.notNull()); // 添加到地图 Map.addLayer(pointFeatures, { color: '#4285F4', pointSize: 4, pointShape: 'circle' }, '经纬度点'); // 设置地图中心 if (pointFeatures.size().gt(0)) { Map.centerObject(pointFeatures, 6); } else { Map.setCenter(0, 0, 2); // 默认视图 print('警告:没有有效的点数据!'); } ``` 这样修改后,应该可以避免`isFinite is not a function`的错误。 §§相关问题§§: 1. 如何将点数据导出为KML文件? 2. 如何在地图上为点添加标签(如序号)? 3. 如何根据某个属性值(如果有)来设置点的颜色? 4. 如何计算这些点的凸包(Convex Hull)? </think># 解决 "lon.isFinite is not a function" 错误 这个错误表明您尝试在非数字对象上调用 `.isFinite()` 方法。让我们分析问题并提供完整解决方案: ## 问题分析 错误信息:`lon.isFinite is not a function` 问题原因: 1. `ee.Number.parse()` 可能返回了非数字对象 2. 输入数据包含无法解析为数字的值 3. Earth Engine API 的 `isFinite()` 方法需要 `ee.Number` 类型 ## 修复后的代码 ```javascript // 加载CSV资产 var points = ee.FeatureCollection('projects/ee-heyunqi98/assets/sampled_data_1'); // 创建点几何(使用更安全的方法) var createPoint = function(feature) { // 获取原始值 var lonValue = feature.get('lon'); var latValue = feature.get('lat'); // 使用 ee.Algorithms.IsFinite 代替 .isFinite() var lon = ee.Number(lonValue); var lat = ee.Number(latValue); // 检查是否有效 var isLonValid = ee.Algorithms.IsFinite(lon); var isLatValid = ee.Algorithms.IsFinite(lat); var isValid = isLonValid.and(isLatValid); // 创建点(仅当坐标有效时) return ee.Algorithms.If(isValid, ee.Feature(ee.Geometry.Point([lon, lat])), null ); }; // 应用函数并过滤无效点 var pointFeatures = points.map(createPoint).filter(ee.Filter.notNull()); // 添加到地图 Map.addLayer(pointFeatures, { color: '#4285F4', pointSize: 4, pointShape: 'circle' }, '经纬度点'); // 设置地图中心 if (pointFeatures.size().gt(0)) { Map.centerObject(pointFeatures, 6); } else { Map.setCenter(0, 0, 2); // 默认视图 print('警告:没有有效的点数据!'); } // 调试:打印第一个点的值 if (pointFeatures.size().gt(0)) { var firstPoint = pointFeatures.first(); print('第一个点的坐标:', firstPoint.geometry().coordinates()); } ``` ## 关键修复说明 ### 1. 使用 `ee.Algorithms.IsFinite` 代替 `.isFinite()` ```javascript var isLonValid = ee.Algorithms.IsFinite(lon); ``` - `ee.Algorithms.IsFinite` 是更安全的方法,可以直接作用于 Earth Engine 对象 - 避免在非数字对象上调用方法 ### 2. 更健壮的类型转换 ```javascript var lon = ee.Number(lonValue); ``` - 使用 `ee.Number()` 直接转换,而不是 `ee.Number.parse()` - 如果转换失败,会返回一个无效数字对象,然后我们使用 `ee.Algorithms.IsFinite` 检测 ### 3. 添加调试信息 ```javascript print('第一个点的坐标:', firstPoint.geometry().coordinates()); ``` - 打印第一个点的坐标验证数据格式 - 帮助诊断数据类型问题 ## 高级解决方案(处理各种数据类型) ```javascript // 增强的点创建函数 var createPointEnhanced = function(feature) { // 获取原始值 var lonValue = feature.get('lon'); var latValue = feature.get('lat'); // 转换为字符串再尝试解析(处理各种数据类型) var lonStr = ee.String(lonValue).trim(); var latStr = ee.String(latValue).trim(); // 尝试转换为数字 var lon = ee.Number.parse(lonStr); var lat = ee.Number.parse(latStr); // 检查有效性 var isLonValid = ee.Algorithms.IsFinite(lon); var isLatValid = ee.Algorithms.IsFinite(lat); var isValid = isLonValid.and(isLatValid); // 范围检查(可选) var inRange = lon.gte(-180).and(lon.lte(180)) .and(lat.gte(-90).and(lat.lte(90))); isValid = isValid.and(inRange); // 创建点 var point = ee.Geometry.Point([lon, lat]); return ee.Algorithms.If(isValid, ee.Feature(point), null ); }; // 应用函数 var pointFeatures = points.map(createPointEnhanced).filter(ee.Filter.notNull()); // 打印统计信息 var totalPoints = points.size(); var validPoints = pointFeatures.size(); print('总点数:', totalPoints); print('有效点数:', validPoints); print('无效点数:', totalPoints.subtract(validPoints)); // 如果有无效点,打印前5个无效点 if (totalPoints.subtract(validPoints).gt(0)) { var invalidPoints = points.map(function(feature) { var isValid = ee.Algorithms.If( ee.Feature(createPointEnhanced(feature)).geometry(), false, true ); return ee.Algorithms.If(isValid, feature.set('is_valid', 0), null); }).filter(ee.Filter.notNull()); print('前5个无效点:', invalidPoints.limit(5)); } ``` 这个高级版本添加了: 1. 字符串转换和修剪处理各种输入格式 2. 范围验证确保经纬度在合理范围内 3. 详细的点数统计 4. 无效点识别和显示功能
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值