gym 100947I (求因子)

    What a Mess

Alex is a very clever boy, after all he is the son of the greatest watchmaker in Odin.

One day, Alex was playing with some old watches and he found n gears, each gear has ai teeth in it. Alex likes to make beautiful pairs of gears, he thinks a pair of gears is beautiful if we attach the two gears together and spin the first gear exactly one rotation, then the other gear spins an integer number of rotations. For example a pair of 8 and 4 is beautiful, whereas a pair of 8 and 5 isn't, neither is pair of 4and 8.

Now Alex is curious, he wants to know how many beautiful pairs are there. Counting is not Alex's thing, so he asked you to help him.

Input

The first line of input contains one integer T: The number of test cases you need to process.

Each test case consists of two lines. The first line is a single integer n: the number of gears Alex has. The second line contains n space separated integers ai: the number if teeth in the ith gear.

1 ≤ n ≤ 104

2 ≤ ai ≤ 106

Output

For each testcase print a single integer: the number of distinct pairs that satisfy the problem statement.

Examples
input
2
5
4 6 7 8 12
6
2 2 2 3 3 4
output
3
7
Note

note that we consider two pair distinct when they differ by at least one gear.

In the first sample the pairs are: (4,8) , (4,12) , (6,12)

题意:

  如果两个数是倍数关系,那么他们就是beautiful,问有多少对beautiful。

思路:

  对于一个数,只求他的每个因子在序列中有几个就可以了,这样就不会重复,注意要先排序,不然有些会计算不到。

代码:

/** @xigua */
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include<climits>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
const int INF = 1e8 + 5;
const ll inf = 1e15 + 5;
const db eps = 1e-9;
int vis[maxn];
int a[maxn];

void solve() {
    memset(vis, 0,sizeof(vis));
    int n; cin >> n;
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        scanf("%d", a + i);
    }
    sort(a+1, a+1+n);
    for (int i = 1; i <= n; i++) {
        int x = a[i];
        for (int j = 1; j * j <= x; j++) {
            if (x % j == 0) {   //是因子
                ans += vis[j];  // 看该因子出现过几次
                int xx = x / j;
                if (xx != j) {
                    ans += vis[xx]; //另一个不同因子,因为因子是成对出现的
                }
            }
        }
        vis[x]++;   //先计算后标记
    }
    cout << ans << endl;
}

int main() {
    //cin.sync_with_stdio(false);
    //freopen("isharp.in", "r", stdin);
    //freopen("isharp.out", "w", stdout);
    int t = 1; cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/ost-xg/p/6398868.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Windows系统中使用stable_baselines3的ppo算法,并结合gym_super_mario_bros玩超级马里奥游戏的优质超参数: ```python import gym_super_mario_bros from gym.wrappers import FrameStack, GrayScaleObservation, \ MarioActionSpaceWrapper from stable_baselines3 import PPO from stable_baselines3.common.vec_env import DummyVecEnv, SubprocVecEnv, VecFrameStack, VecNormalize # 创建超级马里奥游戏环境 def make_env(): env = gym_super_mario_bros.make('SuperMarioBros-1-1-v0') env = MarioActionSpaceWrapper(env) env = FrameStack(env, num_stack=4) env = GrayScaleObservation(env, keep_dim=False) return env # 创建n个游戏环境 def make_envs(n_envs): envs = [make_env() for i in range(n_envs)] envs = VecFrameStack(DummyVecEnv(envs), n_stack=4) envs = VecNormalize(envs) return envs # 定义ppo算法的超参数 policy_kwargs = dict(net_arch=[dict(pi=[256, 256], vf=[256, 256])]) model = PPO("CnnPolicy", make_envs(n_envs=8), policy_kwargs=policy_kwargs, n_steps=2048, batch_size=64, n_epochs=10, gamma=0.99, clip_range=0.2, ent_coef=0.01, verbose=1) # 训练模型 model.learn(total_timesteps=1000000) ``` 在上述代码中,我们使用了DummyVecEnv,这是一个可以将多个单一环境封装成一个向量化环境的类。为了加快训练,我们还使用了SubprocVecEnv,它可以并行地运行多个环境。 我们还使用了VecFrameStack将每个环境的前4个帧堆叠起来,用于提取游戏状态。为了防止模型过拟合,我们使用了VecNormalize对每个环境的观测值进行归一化处理。 最后,我们使用PPO算法进行训练,并设置了一些超参数,例如学习率、折扣因子、剪裁范围、熵系数等。这些超参数的具体设置可以根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值