zoukankan      html  css  js  c++  java
  • luogu P1214 等差数列 枚举

     1 //打出表可以看到,集合内有大约cnt=120000个数。做法是枚举等差数列前两个数,然后再用一个N去判断序列后面每一位是否存在。复杂度高达cnt^2*n,加了玄学优化可以过,应该不是正解。 
     2 #include <cstdio>
     3 #include <vector>
     4 #include <algorithm>
     5 using namespace std;
     6 vector <int> vec;
     7 int n,m;
     8 int vis[130000];
     9 struct dat
    10 {
    11     int a,b;
    12     dat(int _a = 0,int _b = 0):a(_a),b(_b){}
    13     friend bool operator < (dat x,dat y)
    14     {
    15         if (x.b != y.b)
    16             return x.b < y.b;
    17         return x.a < y.a; 
    18     }
    19 };
    20 vector <dat> ans;
    21 int main()
    22 {
    23     scanf("%d%d",&n,&m);
    24     for (int i = 0;i <= m;i++)
    25         for (int j = i;j <= m;j++)
    26             vis[i * i + j * j] = true;
    27     for (int i = 0;i <= 125000;i++)
    28         if (vis[i] == true)
    29             vec.push_back(i);
    30     for (int i = 0;i < vec.size();i++)
    31         for (int j = i + 1;j < vec.size();j++)
    32         {
    33             int a = vec[i],b = vec[j] - vec[i];
    34             //玄学剪枝 
    35             if (a + (n - 1) * b > vec[vec.size() - 1])
    36                 break;
    37             int cnt = 1;
    38             while (vis[a + cnt * b] == true && cnt < n)
    39                 cnt++;
    40             if (cnt == n)
    41                 ans.push_back(dat(a,b));
    42         }
    43     if (ans.size() == 0)
    44     {
    45         printf("NONE
    ");
    46         return 0;
    47     }
    48     sort(ans.begin(),ans.end());
    49     for (int i = 0;i < ans.size();i++)
    50         printf("%d %d
    ",ans[i].a,ans[i].b);
    51     return 0;
    52 }
    心之所动 且就随缘去吧
  • 相关阅读:
    VB几种函数参数传递方法,Variant,数组,Optional,ParamArray
    一些 Windows 系统不常见的 鼠标光标常数
    加载MSCOMCTL.OCX错误处理的几个关键
    如何快速掌握一门技术
    《将博客搬至CSDN》
    日期小demo
    iOS崩溃解决记录
    Swift基础语法
    iOS端APP切图命名规范大全
    PHP案例:学生信息管理系统
  • 原文地址:https://www.cnblogs.com/iat14/p/11143318.html
Copyright © 2011-2022 走看看