• 7.26-Codeforces Round #372 (Div. 2)


    C. Plus and Square Root

    链接:codeforces.com/group/1EzrFFyOc0/contest/716/problem/C

    题型:构造

    题意:起始数 x 为 2,在当前位置 i 可重复加上 i,直到 x 为 (( i + 1 )* k )^ 2  ( k = 1,2,3,4...)则来到位置  i+1 ,问每一步要加上多少次 i 

    题解:做的时候找到规律和构造式一样...但是因为爆ll没过。看了正派题解(https://blog.csdn.net/cmershen/article/details/52624592

    自己再梳理一下:

    在即将跳到下一个 level 的时刻,x 要满足以下条件

    ① x 为 i 的倍数 ,无论是加上 k 个 i 之前还是之后

    ② x 为 ( i + 1 ) 的倍数

    ③ x 为完全平方数

    则设 x = i  ^ 2 * ( i + 1 ) ^ 2 ,由于递推,在刚抵达 i 时 x ' = i *( i - 1 ),所以 ans = (  x - x ' ) / i = i * i * i + 2 * i * i + 1 , 又 i = 1 时 起始值为 2 而非 i *( i - 1 ),所以要特判。

    其他的满足条件的构造式都符合,解不唯一。

    原因:迭代器设为int后爆ll,四次方爆ll,请记住。

    代码:

    /*找规律迭代器设为int后爆ll,四次方爆ll*/
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <stdio.h>
    #include <string>
    #include <map>
    #include <vector>
    #include <cmath>
    #include <set>
    #define ll long long
    #define PI 3.1415926535
    using namespace std;
    const int inf=2e5+10;
    //map<int,int> s;
    bool cmp(const string& a,const string& b)
    {
      return a.length()<b.length();
    }
    map<char,int>mp;
    int main()
    {
      ll n;
      cin>>n;
      ll t=2;
      for(ll i=1;i<=n;i++)
      {
           ll temp;
          temp=i*(i+1);
           ll ans;
           //ans=(temp*temp-t)/i;
           ans=temp/i*temp-t/i;
           t=temp;
         cout<<ans<<endl;
    
      }
    }
    /*构造,即规律的展开式*/
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <stdio.h>
    #include <string>
    #include <map>
    #include <vector>
    #include <cmath>
    #include <set>
    #define ll long long
    #define PI 3.1415926535
    using namespace std;
    const int inf=2e5+10;
    //map<int,int> s;
    bool cmp(const string& a,const string& b)
    {
      return a.length()<b.length();
    }
    map<char,int>mp;
    int main()
    {
      ll n;
      cin>>n;
      ll t=2;
      cout<<2<<endl;
      for(ll i=2;i<=n;i++)
      {
           //ll temp;
          //temp=i*(i+1);
           ll ans;
           //ans=(temp*temp-t)/i;
           //ans=temp/i*temp-t/i;
           ans=i*i*i+2*i*i+1;
           //t=temp;
         cout<<ans<<endl;
    
      }
    
    }
  • 相关阅读:
    Java hello/hi的简单的网络聊天程序
    案例分析:设计模式与代码的结构特性
    网络相关的命令工具研究报告
    如何提高程序员的键盘使用效率
    分析一套源代码的代码规范和风格并讨论如何改进优化代码
    用例建模Use Case Modeling
    结合工程实践选题调研分析同类软件产品
    领域建模
    SecureCRT 连接虚拟机Linux
    用css固定div层在页面顶部和底部(兼容IE6)
  • 原文地址:https://www.cnblogs.com/LLbinGG/p/9370977.html
Copyright © 2020-2023  润新知