Codeforces D. Very Suspicious(思维递推)
作者:
我不会取名字啊啊
,
2022-05-09 11:08:51
,
所有人可见
,
阅读 299
题 目 链 接
思维递推,真的是难捏...(找不出规律...)
题解:
https://zhuanlan.zhihu.com/p/510572963
画个图,递推一下,发现每添加一条线增加的三角形个数是有规律的(三个一组,每组由前面+2)
[0 2 4][4 6 8][8 10 12]
所以,我们定义cnt[i]为添加第i条线时,能产生的三角形个数;
ans[i]为添加了i条线时,总共的三角形个数,ans[i] = ans[i-1] + cnt[i]
那么对于需要产生x个三角形,我们直接在ans里面二分就行.
#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<map>
#include<cmath>
#include<algorithm>
#include<deque>
#include<stack>
#include <unordered_map>
#define IOS ios::sync_with_stdio(false),cin.tie(0);
using namespace std;
#define pb push_back
#define coutt cout<<"------------"<<endl;
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<char, pair<int, int> > PCII;
typedef pair<int, pair<int, int> > PIII;
const int maxn = 2e5 + 7;
const int N = 2e5+7;
const int M = 1e6+7;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = 3.14159265359;
ll gcd(ll a,ll b) {return b==0 ? a : gcd(b,a%b);}
ll qmi(ll a,ll b,ll p) {ll ans = 1; while(b) { if(b & 1) ans = ans * a % p; a = a * a % p; b >>= 1; } return ans;}
ll cnt[maxn];
ll ans[maxn];
int n;
void init()
{
n = 0;
while(ans[n] < mod)
{
n ++;
if(n % 3 == 1) cnt[n] = cnt[n-1];
else cnt[n] = cnt[n-1] + 2;
ans[n] = ans[n-1] + cnt[n];
}
}
int main()
{
init();
int t;
cin>>t;
while(t--)
{
int x;
cin>>x;
int num = lower_bound(ans,ans+1+n,x) - ans;
cout<<num<<endl;
}
return 0;
}