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;

  }

}

猜你喜欢

转载自www.cnblogs.com/LLbinGG/p/9370977.html