题目大意:有一个$n\times m$的方格图,求其中所有的格点正方形完整包含的小方格个数,多组询问。$n,m\leqslant 10^6$

题解:令$n\leqslant m$。有一个显然的式子:
$$
ans=\sum\limits_{i=1}^n(n-i+1)(m-i+1)f(i)
$$
$f(i)$表示可以完整包含在$i\times i$的正方形中且顶点在这个正方形边上的正方形所包含的小方格总数。可以分选的正方形和$i\times i$正方形重合和边转动$j$格来计算
$$
f(n)=n^2+\sum\limits_{i=1}^{n-1}[(n-2\max\{i,n-1\})^2+g(i,n-i)]
$$
其中$(n-2\max\{i,n-1\})^2$是转动$i$格后正中间的完整小方格,$g(i,n-i)$表示四周的$4$个小直角三角形中包含的完整小方格个数。

比如计算左上角的直角三角形,发现完整的小方格的左上角满足在直角三角形的斜边上或三角形内。三角形内的点可以用皮克定理来解决,令三角形两直角边为$n,m$,则三角形内的点个数为$\dfrac{nm-n-m+2-\gcd(n,m)}2$,再加上在斜边上的点,则一个直角三角形内的方格数为$\dfrac{nm-m-n+gcd(n,m)}2$。

把$n=i,m=n-i$带入式子
$$
\begin{align*}
f(n)=&n^2+\sum\limits_{i=1}^{n-1}[(n-2\max\{i,n-1\})^2+g(i,n-i)]\\
    =&n^2+\sum\limits_{i=1}^{n-1}(n-2\max\{i,n-1\})^2+\sum\limits_{i=1}^{n-1}g(i,n-i)\\
    =&n^2+\sum\limits_{i=1}^{\left\lfloor\frac{n-1}2\right\rfloor}(n-2i)^2+\sum\limits_{i=\left\lfloor\frac{n-1}2\right\rfloor+1}^{n-1}(2i-n)^2\\
    &+\sum\limits_{i=1}^{n-1}\dfrac{i(n-i)-(n-i)-i+\gcd(i,n-i)}2\\
    =&n^2+2\sum\limits_{i=1}^{\left\lfloor\frac{n-1}2\right\rfloor}(n-2i)^2+\sum\limits_{i=1}^{n-1}\dfrac{in-i^2-n+\gcd(i,n)}2
\end{align*}
$$
其中$n^2$可以快速计算,$\sum\limits_{i=1}^{\left\lfloor\frac{n-1}2\right\rfloor}(n-2i)^2$可以前缀和解决,问题在$\sum\limits_{i=1}^{n-1}\dfrac{in-i^2-n+\gcd(i,n)}2$部分。令$h(n)=\sum\limits_{i=1}^{n-1}\dfrac{in-i^2-n+\gcd(i,n)}2$,$sgcd(n)=\sum\limits_{i=1}^{n-1}\gcd(i,n)$
$$
h(n)=\dfrac12[\sum\limits_{i=1}^{n-1}(in-i^2-n)+sgcd(n)]\\
\begin{align*}
h(n-1)&=\dfrac12[\sum\limits_{i=1}^{n-2}(i(n-1)-i^2-(n-1))+sgcd(n-1)]\\
    &=\dfrac12[\sum\limits_{i=1}^{n-2}(in-i^2-n-i+1)+sgcd(n-1)]\\
\end{align*}\\
$$
$$
\begin{align*}
h(n)=&h(n-1)+\dfrac12[\sum\limits_{i=1}^{n-2}(i-1)+(n-1)n-(n-1)^2-n\\
    &-sgcd(n-1)+sgcd(n)]\\
    =&h(n-1)+\dfrac12[\left(\sum\limits_{i=0}^{n-3}i\right)-1-sgcd(n-1)+sgcd(n)]\\
    =&h(n-1)+\dfrac12[\left(\dfrac{(n-2)(n-3)}2\right)-1-sgcd(n-1)+sgcd(n)]
\end{align*}
$$

其他部分都可以快速求出,问题在求$sgcd(n)$
$$
\begin{align*}
sgcd(n)&=\sum\limits_{i=1}^{n-1}\gcd(i,n)\\
    &=\left(\sum\limits_{d|n}\dfrac nd\varphi(d)\right)-n
\end{align*}
$$
全部预处理出来即可。

卡点:

C++ Code:

#include <cstdio>
#include <algorithm>
#include <iostream>
#define mul(a, b) (static_cast<long long> (a) * (b) % mod)
const int mod = 1e9 + 7, maxn = 1e6 + 10, half = (mod + 1) / 2;
inline void reduce(int &x) { x += x >> 31 & mod; } int gcd(int a, int b) {
if (!b) return a;
return gcd(b, a % b);
}
inline int sqr(int x) { return mul(x, x); } int g[maxn], sumgcd[maxn], phi[maxn], plist[maxn / 2], ptot;
int pre[maxn], R[maxn], T[maxn], preF[maxn];
bool notp[maxn];
int Q; int F(int n) {
int ans = g[n];
reduce(ans += sqr(n) - mod);
reduce(ans += pre[n - 2] - mod);
reduce(ans += pre[n - 2] - mod);
return ans;
}
void sieve(int N) {
phi[1] = 1;
for (int i = 2; i <= N; i++) {
if (!notp[i]) phi[plist[ptot++] = i] = i - 1;
for (int j = 0, t; j < ptot && (t = i * plist[j]) <= N; j++) {
notp[t] = true;
if (i % plist[j] == 0) {
phi[t] = phi[i] * plist[j];
break;
}
phi[t] = phi[i] * phi[plist[j]];
}
}
for (int i = 1; i <= N; ++i) {
for (int j = i + i; j <= N; j += i) reduce(sumgcd[j] += mul(phi[j / i], i));
}
for (int i = 4; i <= N; ++i) {
g[i] = (1ll * (i - 3) * (i - 2) / 2 - 1 - sumgcd[i - 1] + sumgcd[i]) % mod;
reduce(g[i]);
g[i] = mul(g[i], half);
reduce(g[i] += g[i - 1] - mod);
}
for (int i = 1; i <= N; ++i) g[i] = mul(g[i], 4);
pre[1] = 1;
for (int i = 2; i <= N; ++i) reduce(pre[i] = pre[i - 2] + sqr(i) - mod);
for (int i = 1; i <= N; ++i) reduce(preF[i] = preF[i - 1] + F(i) - mod);
for (int i = 1; i <= N; ++i) {
reduce(R[i] = R[i - 1] + preF[i - 1] - mod);
reduce(R[i] += F(i) - mod);
}
for (int i = 1; i <= N; ++i) {
reduce(T[i] = T[i - 1] + preF[i - 1] - mod);
reduce(T[i] += R[i - 1] - mod);
reduce(T[i] += R[i - 1] - mod);
reduce(T[i] += F(i) - mod);
}
}
int solve(int n, int m) {
int ans = T[n];
reduce(ans += mul(R[n], m - n) - mod);
return ans;
} int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
sieve(1000005);
std::cin >> Q;
while (Q --> 0) {
static int n, m;
std::cin >> n >> m;
if (n > m) std::swap(n, m);
std::cout << solve(n, m) << '\n';
}
return 0;
}

  

CF449E Jzzhu and Squares的更多相关文章

  1. soj#552 449E Jzzhu and Squares

    分析 https://www.cnblogs.com/Memory-of-winter/p/11209128.html 代码 #include<bits/stdc++.h> using n ...

  2. CF449 (Div. 1简单题解)

    A .Jzzhu and Chocolate pro:现在给定一个大小为N*M的巧克力,让你横着或者竖着切K刀,都是切的整数大小,而且不能切在相同的地方,求最大化其中最小的块. (N,M,K<1 ...

  3. Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪婪、数学)

    主题链接:http://codeforces.com/problemset/problem/449/A ------------------------------------------------ ...

  4. Codeforces Round #257 (Div. 2) C. Jzzhu and Chocolate

    C. Jzzhu and Chocolate time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. cf 450c Jzzhu and Chocolate

    Jzzhu and Chocolate time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces 450C:Jzzhu and Chocolate(贪心)

    C. Jzzhu and Chocolate time limit per test: 1 seconds memory limit per test: 256 megabytes input: st ...

  7. [LeetCode] Word Squares 单词平方

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  8. 卡通图像变形算法(Moving Least Squares)附源码

    本文介绍一种利用移动最小二乘法来实现图像变形的方法,该方法由用户指定图像中的控制点,并通过拖拽控制点来驱动图像变形.假设p为原图像中控制点的位置,q为拖拽后控制点的位置,我们利用移动最小二乘法来为原图 ...

  9. Leetcode: Word Squares && Summary: Another Important Implementation of Trie(Retrieve all the words with a given Prefix)

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

随机推荐

  1. async、await总结

    一.async用法 async作为一个关键字放到函数前面,用于表示函数是一个异步函数.异步函数也就意味着该函数的执行不会阻塞后面代码的执行. 异步函数语法很简单,就是在函数前面加上async 关键字, ...

  2. 前端微信小程序资讯类仿今日头条微信小程序

    需求描述及交互分析设计思路和相关知识点新闻频道滑动效果设计首页新闻内容设计首页新闻详情页设计我的界面列表式导航设计系统设置二级界面设计 设计思路(1)设计底部标签导航,准备好底部标签导航的图标和建立相 ...

  3. 安卓入门教程(十三)-Activity

    已经发表个人公众号 什么是Activity? Android是由Activity,Service,Content,Provider等组件组成,其中要讲的就是Activity组件,这是最基本,且常用的组 ...

  4. mysql 选出前五个元素

    mysql> select * from test; +----+----------+-------+-----------+ | id | name | score | subject | ...

  5. mysql查询、子查询、连接查询

    mysql查询.子查询.连接查询 一.mysql查询的五种子句 where子句(条件查询):按照“条件表达式”指定的条件进行查询. group by子句(分组):按照“属性名”指定的字段进行分组.gr ...

  6. 转载:Base64编解码介绍

    https://www.liaoxuefeng.com/wiki/897692888725344/949441536192576 Base64是一种用64个字符来表示任意二进制数据的方法. 用记事本打 ...

  7. CEF 远程调试

    转载:https://www.cnblogs.com/TianFang/p/9906786.html 转载:https://stackoverflow.com/questions/29117882/d ...

  8. http请求传参问题解决

    1.接口参数:使用form-data形式传参如果值太多就会报错误. 2.接口参数:使用form-data形式传参如果值太多就会报错误.这样前端可以传json就可以避免这样问题

  9. js 经常用于条件判断 大于等于0 的正整数

    /^\d+(?=\.{,}\d+$|$)/.test() // true 转:https://www.jianshu.com/p/feef5e62dd67

  10. flink入门(一)——基本原理与应用场景

    一.简介 1.简介 flink是一个开源的分布式流处理框架 优势:高性能处理.高度灵活window操作.有状态计算的Exactly-once等 详情简介,参考官网:https://flink.apac ...