078. Hashing (25)

本文详细介绍了如何使用哈希表和二次探测算法进行数据插入操作,包括哈希函数的选择、二次探测的实现及质数表大小的优化策略。

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

078. Hashing (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be "H(key) = key % TSize" where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.

Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (<=104) and N (<=MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print "-" instead.

Sample Input:
4 4
10 6 4 15
Sample Output:
0 1 4 -
提供Msize大小的哈希表(编号从0~Msize-1)  有N个要填入的值
接着是N个要填入的值 key1  key2 ……
首先找到一个质素,prime>=Msize;这个质素是最接近Msize的;令这个Msize=这个质素(只能被1和它本身整除的正整数);
hash    
   按照输入的key顺序,inner从0到Msize-1如果pos=(key+inner*inner)%Msize;
                                    一发现有table[pos]还没有放东西,就输出这个pos并置满;
                                    若都有放东西,就“-”

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
8月17日 00:19答案正确251078C++ (g++ 4.7.2)21384datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确118012/12
1答案正确12483/3
2答案正确13085/5
3答案正确213845/5

#include<iostream>  
#include<vector> 
#include<math.h>
using namespace std;
int largerMsizeFirstPrime(int Msize)
{
  if (Msize <= 2)return 2;
  bool nofind=true;
  int i;
  while (nofind)
  {
    int eNd = sqrt(Msize); 
    for (i = 2; i <= eNd&&Msize%i != 0; i++)
      ;
    if (i> eNd)nofind = false;
    else Msize++;

  }
  return Msize;
}
int main()
{
  int N,Msize,data,pos,index;  
  cin >> Msize >>N; 
  Msize = largerMsizeFirstPrime(Msize);
  vector<bool>table(Msize, true); 
  for (index = 0; index < N;index++)
  {
        cin >> data;   
      int inner = 0;
      do
      {
        pos = (data + inner*inner) % Msize;
        inner++;
      } while (!table[pos] && inner < Msize);
      if (inner== Msize)
      {
        if (index>0)cout << " ";
        cout <<"-";
      }
      else
      { 
        table[pos] = false;
        if (index>0)cout << " ";
        cout << pos;
      }  
  }
  cout << endl;
    system("pause");
  return 0;
}


### PyTorch 中 `torch.nn.utils.weight_norm` 的弃用情况 在 PyTorch 的开发历程中,某些功能可能会被标记为过时并最终移除。关于 `torch.nn.utils.weight_norm` 的具体弃用版本信息,在官方文档和社区讨论中并未明确指出其完全废弃的时间点[^1]。然而,可以确认的是,自 PyTorch 1.7 版本起,`weight_norm` 功能已被推荐使用更灵活的替代方案——通过 `nn.Parameter` 或者其他模块化设计来实现权重标准化的效果。 #### 替代方案 对于需要实现类似 `weight_norm` 功能的需求,开发者通常会采用以下几种方式: 1. **手动实现权重规范化** 可以通过定义新的参数并将原始权重分解为其大小和方向部分来模拟 `weight_norm` 的行为。例如: ```python import torch import torch.nn as nn class WeightNormLinear(nn.Module): def __init__(self, in_features, out_features): super(WeightNormLinear, self).__init__() self.weight_v = nn.Parameter(torch.randn(out_features, in_features)) self.weight_g = nn.Parameter(torch.ones(out_features)) self.bias = nn.Parameter(torch.zeros(out_features)) def forward(self, x): weight_normalized = self.weight_g.unsqueeze(-1) * (self.weight_v / torch.norm(self.weight_v, dim=-1, keepdim=True)) return torch.matmul(x, weight_normalized.t()) + self.bias ``` 2. **利用 Spectral Normalization** 如果目标是为了控制模型的 Lipschitz 常数,可以考虑使用谱归一化(Spectral Normalization),它可以通过 `torch.nn.utils.spectral_norm` 实现类似的约束效果[^4]。 3. **基于最新 API 进行重构** 自 PyTorch 高级优化器引入以来,许多旧的功能逐渐被淘汰。因此建议重新评估需求,并尝试借助更高层次的设计模式完成相同的目标。 需要注意的是,尽管目前尚未有确切记录表明某个特定版本正式删除了该函数,但在实际项目维护过程中应尽量避免依赖已标注为 deprecated 的特性[^5]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值