1. 程式人生 > >7.26-Codeforces Round #372 (Div. 2)

7.26-Codeforces Round #372 (Div. 2)

叠代 但是 roo 題意 tails net tps 3.1 level

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;

  }

}

7.26-Codeforces Round #372 (Div. 2)