Gym 100541 B. 思维题 找规律

本文介绍了一种计算给定正整数n下特定数学序列和的方法,通过将数据分块来优化计算过程,每一块包含连续的相同数值,算法在每次迭代中更新相同数值的长度和值,最后输出计算总和对10^6取模的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Write a program to compute the following sum S given a positive integer n:

, where  is the largest integer not greater than x.

Input 

The input file consists of several datasets. The first line of the input file contains the number of datasets which is a positive integer and is not greater than 30. The following lines describe the datasets.

Each dataset contains a positive integer n (n ≤ 1012) written on a separate line.

Output 

For each dataset, write in one line the remainder of the computed sum S divided by 106. 

Input 

2
1
5

Output

1

10 

My Solution

写几个n试试看, 会有连续的数字出现, 

比如

n == 5, 5 2 1 1 1

n == 8, 8 4 2 2 1 1 1 1

相当于把这些数据分成好多块, 最多sqrt(n)份,区别于数据结构分块的另外一种分块吧^_^

len 表示相同数字的个数, 然后i表示这些相同val (LL)的第一个数的下标, 

ans + len*val //然后注意每一步取模

i += len; //直接跳到下一种val的第一个数

其中 len = n / val - i + 1

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int Hash = 1e6;
 
inline LL mod(LL x)
{
    return x - x/Hash*Hash;
}
 
int main()
{
    #ifdef LOCAL
    freopen("a.txt", "r", stdin);
    //freopen("b.txt", "w", stdout);
    #endif // LOCAL
    int T;
    LL n, val, len, ans;   //len 表示一串相同的 val 的长度
    scanf("%d", &T);
    while(T--){
        ans = 0;
        scanf("%I64d", &n);
        for(LL i = 1, j; i <= n; i += len){
            val = n/i;
            len = n/val - i + 1;
            ans = mod(ans + mod(len*val));
        }
        printf("%I64d\n", ans);
 
    }
    return 0;
}

 来源:https://blog.youkuaiyun.com/ProLightsfxjh/article/details/52031832

### 使用 `gym.spaces.Box` 定义动作空间 在OpenAI Gym环境中定义连续的动作空间通常会使用到 `gym.spaces.Box` 类。此类允许创建一个多维的盒子形状的空间,其边界由低限(low)和高限(high)参数指定[^1]。 对于给定的例子,在类 `ActionSpace` 中静态方法 `from_type` 返回了一个基于输入类型的行动空间实例: 当 `space_type` 是 `Continuous` 时,返回的是一个三维向量形式的动作空间对象,该对象表示三个维度上的取值范围分别为 `[0.0, 1.0]`, `[0.0, 1.0]`, 和 `[-1.0, 1.0]` 的实数集合,并且数据类型被设定为了 `np.float32`: ```python import numpy as np import gym class ActionSpace: @staticmethod def from_type(action_type: int): space_type = ActionSpaceType(action_type) if space_type == ActionSpaceType.Continuous: return gym.spaces.Box( low=np.array([0.0, 0.0, -1.0]), high=np.array([1.0, 1.0, 1.0]), dtype=np.float32, ) ``` 此段代码展示了如何通过传递最低限度(`low`)数组以及最高限度(`high`)数组来初始化一个新的Box实例,从而构建出一个具有特定界限的多维连续数值区间作为环境可能采取的一系列合法行为的选择集的一部分。 另外值得注意的是,每个环境都应当具备属性 `action_space` 和 `observation_space` ,这两个属性应该是继承自 `Space` 类的对象实例;Gymnasium库支持大多数用户可能会需要用到的不同种类的空间实现方式[^2]。 #### 创建并测试 Box 动作空间的一个简单例子 下面是一个简单的Python脚本片段用于展示怎样创建和验证一个基本的 `Box` 空间成员资格的方法: ```python def check_box_space(): box_space = gym.spaces.Box(low=-1.0, high=1.0, shape=(2,), dtype=np.float32) sample_action = box_space.sample() # 获取随机样本 is_valid = box_space.contains(sample_action) # 检查合法性 print(f"Sampled action {sample_action} within bounds? {'Yes' if is_valid else 'No'}") check_box_space() ``` 上述函数首先建立了一个二维的 `-1.0` 到 `1.0` 范围内的浮点型 `Box` 空间,接着从中抽取了一组随机样本来检验它确实位于所规定的范围内。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值