为了能到远方,脚下的每一步都不能少.|

园龄:粉丝:关注:

P4139TriangleXor

数学 #计数 #容斥

分为 4 个部分计算

  1. 上面的按奇偶性分类
  2. 下面的按每一层容斥,即 sizi2×sizi1+sizi2
  3. 左右直接对每一块计算面积
// Author: xiaruize
const int INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;
int n;
double res = 0;
double calc(int i)
{
return (double)i / ((double)n + (double)i) * (double)i / 2.0;
}
void solve()
{
cin >> n;
for (int i = 2; i <= n; i += 2)
{
res = res + (double)(n - i + 1) * (calc(i) - calc(i - 1) * 2 + calc(i - 2));
}
// debug(res);
rep(i, 1, (n + 1) / 2)
{
res += ((double)n / 4) * (((double)(i * 2 - 1) / ((double)n + (double)(i * 2 - 1)) - (double)(i * 2 - 2) / ((double)n + (double)(i * 2 - 2)))) * 2.0 * 2.0;
}
if (n % 2 == 0)
res += (double)n / 4.0;
cout << (int)floor(res) << endl;
}
#ifndef ONLINE_JUDGE
bool end_of_memory_use;
#endif
signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int testcase = 1;
// cin >> testcase;
while (testcase--)
solve();
#ifndef ONLINE_JUDGE
cerr << "Memory use:" << (&end_of_memory_use - &start_of_memory_use) / 1024.0 / 1024.0 << "MiB" << endl;
cerr << "Time use:" << (double)clock() / CLOCKS_PER_SEC * 1000.0 << "ms" << endl;
#endif
return 0;
}

本文作者:xiaruize's Blog

本文链接:https://www.cnblogs.com/xiaruize/p/18121247

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   xiaruize  阅读(4)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起