首页 > 其他分享 >CodeForces 1924C Fractal Origami

CodeForces 1924C Fractal Origami

时间:2024-01-28 11:57:38浏览次数:24  
标签:node const res ll CodeForces sqrt Origami Fractal mod

洛谷传送门

CF 传送门

对这种题一点办法都没有。。。

可以手动折叠发现 \(n = 3\) 时 \(M = 2 + 2 \sqrt{2}, V = 2 + 4 \sqrt{2}\)。于是大胆猜结论,第二次折叠开始,每次产生的山谷和山峰的长度相等。

为什么呢?考虑从第二次折叠开始,设当前纸的层数为 \(k\)(事实上若当前是第 \(i\) 次折叠,\(k = 2^{i - 1}\))。则奇数层的纸展开后是山谷,偶数层的纸展开后是山峰。所以 \(V = M + 2 \sqrt{2}\) 恒成立。

这意味着我们只用计算 \(n\) 次折叠后的总折痕长度 \(V + M\),就能算出 \(M\) 和 \(V\) 的值。考虑每次折叠,纸的每一层的折痕长度为上一次折叠时 \(\times \frac{1}{\sqrt{2}}\),但是纸的层数为上一次折叠时 \(\times 2\)。所以每次折叠,总折痕长度为上一次的 \(\sqrt{2}\) 倍。于是 \(M = \sum\limits_{i = 0}^{n - 2} 2 \sqrt{2}^i, V = 2 \sqrt{2} + \sum\limits_{i = 0}^{n - 2} 2 \sqrt{2}^i\)。

至此直接套用等比数列求和公式 \(s = \frac{a (1 - q^n)}{1 - q}\) 即可出答案。由于需要快速幂,时间复杂度为 \(O(\sum \log n)\)。

实现时封装一个 \(a + b \sqrt{2}\) 的类会好写很多。

code
// Problem: C. Fractal Origami
// Contest: Codeforces - Codeforces Round 921 (Div. 1)
// URL: https://codeforces.com/contest/1924/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const ll mod = 999999893;

inline ll qpow(ll b, ll p) {
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = res * b % mod;
		}
		b = b * b % mod;
		p >>= 1;
	}
	return res;
}

ll n;

struct node {
	ll a, b;
	node(ll x = 0, ll y = 0) : a(x), b(y) {}
};

inline node operator + (const node &a, const node &b) {
	return node((a.a + b.a) % mod, (a.b + b.b) % mod);
}

inline node operator - (const node &a, const node &b) {
	return node((a.a - b.a + mod) % mod, (a.b - b.b + mod) % mod);
}

inline node operator * (const node &a, const node &b) {
	return node((a.a * b.a + a.b * b.b * 2) % mod, (a.a * b.b + a.b * b.a) % mod);
}

inline node operator / (const node &x, const node &y) {
	ll a = x.a, b = x.b, c = y.a, d = y.b;
	return node((a * c - b * d * 2 % mod + mod) % mod * qpow((c * c - d * d * 2 % mod + mod) % mod, mod - 2) % mod, (b * c - a * d % mod + mod) % mod * qpow((c * c - d * d * 2 % mod + mod) % mod, mod - 2) % mod);
}

inline node qpow(node b, ll p) {
	node res(1, 0);
	while (p) {
		if (p & 1) {
			res = res * b;
		}
		b = b * b;
		p >>= 1;
	}
	return res;
}

void solve() {
	scanf("%lld", &n);
	node a = node(1, 0) - qpow(node(0, 1), n - 1), b = node(1, 0) - node(0, 1);
	node x = a * node(2, 0) / b;
	node y = x + node(0, 2);
	node res = x / y;
	printf("%lld\n", res.b);
}

int main() {
	int T = 1;
	scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:node,const,res,ll,CodeForces,sqrt,Origami,Fractal,mod
From: https://www.cnblogs.com/zltzlt-blog/p/17992673

相关文章

  • CodeForces 1924B Space Harbour
    洛谷传送门CF传送门不知道为什么好像大家在这题上花了挺久的。发现对于一对相邻的港口\((x_i,x_{i+1})\),\(x\in(x_i,x_{i+1})\)的花费是\(y_i(x_{i+1}-x)\)。拆开得\(y_ix_{i+1}-y_ix\)。考虑用set维护所有港口,这样可以知道一个港口左边和右边的港......
  • CodeForces 1924D Balanced Subsequences
    洛谷传送门CF传送门发现去掉匹配的\(2k\)个括号后,剩下的串一定形如\())\ldots)((\ldots(\),其中右括号数量为\(a=m-k\),左括号数量为\(b=n-k\)。考虑把剩下的串像\())\ldots)\mid((\ldots(\)一样分成两半。枚举左半边加入了\(\frac{i}{2}\)对匹配,则......
  • hey_left 18 Codeforces Round 920 (Div. 3)
    题目链接A.根据正方形4个角的特性,可以把它们排序处理,得到长和高,相乘得面积#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=1e5+10;boolcmp(pair<int,int>x,pair<int,int>y){if(x.first==y.first)returnx.second<y.second;e......
  • Codeforces Educational Round
    CodeforcesEducationalRoundEducationalCodeforcesRound160(RatedforDiv.2)(A-D)https://www.cnblogs.com/ComistryMo/articles/17920495.htmlEducationalCodeforcesRound161(RatedforDiv.2)(A-E)https://www.cnblogs.com/ComistryMo/articles/17983580......
  • Educational Codeforces Round 65 (Rated for Div. 2)C. News Distribution(模拟,计算的
    这道题目明显和出现4次的数和出现2次的数的个数有关系,只需要在每次更新之后维护这两个信息即可,我们在算出现2次的数的个数时其实会把出现4次的数的个数会把出现2次的数的个数+2,在判断时需要考虑这一点。也就是\(cnt2>=4\&\&cnt4>=1\)时才有解#include<bits/stdc++.h>#definer......
  • CodeForces 995F Cowmpany Cowmpensation
    洛谷传送门CF传送门考虑一个显然的树形dp,设\(f_{u,i}\)为\(u\)结点染颜色\(i\)的方案数,有\(f_{u,i}=\prod\limits_{v\inson_u}\sum\limits_{j=1}^if_{v,j}\)。前缀和后可得\(f_{u,i}=f_{u,i-1}+\prod\limits_{v\inson_u}f_{v,i}\)。发现\(f_......
  • Codeforces Round 912 (Div
    AHalloumiBoxes题目大意给定一个数组A,我们可以对数组惊醒多次操作,操作如下:我们可以将数组中的某一段倒置,但是长度不能超过K,例如:反转子数组意味着选择两个索引i和j(其中1<=i<=j<=n)并将数组\[a_1,a_2,…,a_n\]改为\[a_1,a_2,…,a_{i−1},a_{j},a_{j−1},…......
  • Codeforces Round 913 (Div
    ARook题目大意给一个国际象棋棋盘,有t次询问,每次询问给定一个棋子坐标s例如d4.问:输出这个棋子上下左右四个方向的坐标解题思路两个for循环暴力求解代码#include<bits/stdc++.h>#defineintlonglong#defineendl'\n'constintINF=0x3f3f3f3f;voidso......
  • Codeforces Round 914 (Div
    AForked!题目大意给定王后和国王的位置,马可以先朝一个方向走a步,再朝另一个方向走b步问:马有多少个位置可以同时走到皇后和国王解题思路就无脑遍历一下马能走到国王和皇后的位置然后再判断下有没有相同的位置代码#include<bits/stdc++.h>#defineintlonglong......
  • Codeforces Round 916 (Div
    AProblemsolvingLog题目描述给一个整数n,字符串s,字符串中s[i]表示第i分钟解决第s[i]题.问题A需要1分钟解决,问题B需要2分钟解决,以此类推.问:可以解决多少题?解题思路遍历字符串,统计问题A--Z用了多少时间解决.最后在遍历数组,判断问题A--Z是否满足解决时间.代......