可推出$a_n = n^2+n, $ 设\(S_n = \sum_{i=1}^{n} a_i\) 则 \(S_n = \frac{n(n+1)(2n+1)}{6} + \frac{n(n+1)}{2}\)

需要求出\([1,N]\)中与\(M\)互质的下标的和

可以容斥计算答案,\(O(1)\)时间算出\(S_n\),需要减去与\(M\)非互质的下标\(a_i\)

对于\(M\)的每一种质因数的组合\(k\),可以求出\(b_kn = (kn)^2+kn\),则也可以\(O(1)\)得到在\(bn在[1-N]\)中的和.

根据容斥原理,奇数个质因子的组合要减去,偶数个的加回

#include <iostream>
#include <string>
#include <cmath>
#include <stdio.h>
#include <vector>
using namespace std;
const int mod= 1e9+7;
typedef long long LL;
vector<LL> fac; LL qpow(LL a,LL n)
{
LL res=1;
while(n){
if(n&1) res = res*a %mod;
a = a*a %mod;
n>>=1;
}
return res;
} LL solve(LL n,LL m)
{
LL rev6 = qpow((LL)6,mod-2);
LL rev2 = qpow((LL)2,mod-2);
fac.clear();
LL tmp = m;
for(LL i=2;i*i<=tmp;++i){
if(tmp%i==0){
fac.push_back(i);
while(tmp%i==0) tmp/=i;
}
}
if(tmp>1) fac.push_back(tmp); LL res = n *(n+1) %mod *(2*n+1) %mod *rev6 %mod;
LL t = n*(n+1) %mod *rev2 %mod;
res = (res+t) %mod; int len = fac.size();
int tot = 1<<len;
for(int i=1;i<tot;++i){
int cnt = __builtin_popcount(i);
LL tmp =1;
for(int j = 0;j<len;++j){
if(i&(1<<j)){
tmp *= fac[j];
}
}
LL nn = n/tmp;
LL pt = (nn)%mod *(nn+1)%mod *(2*nn+1) %mod*rev6%mod;
pt = pt*tmp %mod *tmp %mod;
pt = (pt+nn*(tmp+tmp*nn)%mod*rev2%mod) %mod;
if(cnt&1) res = (res-pt+mod)%mod;
else res = (res+pt)%mod;
}
return res;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
LL N,M;
while(scanf("%lld %lld",&N,&M)==2){
LL res = solve(N,M);
printf("%lld\n",res);
}
return 0;
}

ACM-ICPC 2018 沈阳赛区网络预赛 G. Spare Tire (容斥原理)的更多相关文章

  1. ACM-ICPC 2018 沈阳赛区网络预赛 G Spare Tire (素因子分解+容斥)

    . 样例输入复制 4 4 样例输出复制 14 #include<bits/stdc++.h> using namespace std; typedef long long ll; cons ...

  2. ACM-ICPC 2018 沈阳赛区网络预赛 G Spare Tire(容斥)

    https://nanti.jisuanke.com/t/31448 题意 已知a序列,给你一个n和m求小于n与m互质的数作为a序列的下标的和 分析 打表发现ai=i*(i+1). 易得前n项和为 S ...

  3. ACM-ICPC 2018 沈阳赛区网络预赛 G. Spare Tire

    这题很好啊,好在我没做出来...大概分析了一下,题目大概意思就是求 问所有满足1<=i<=n且i与m互素的ai之和 最开始我们队的做法是类似线性筛的方法去筛所有数,把数筛出来后剩下数即可, ...

  4. ACM-ICPC 2018 沈阳赛区网络预赛 G 容斥原理

    https://nanti.jisuanke.com/t/31448 解析 易得an=n*n+n O(1)得到前n项和  再删除与m不互素的数  我们用欧拉函数求出m的质因数  枚举其集合的子集 进行 ...

  5. 【ACM-ICPC 2018 沈阳赛区网络预赛 G】Spare Tire

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 让你求出1..n中和m互质的位置i. 让你输出∑ai 这个ai可以oeis一波. 发现是ai = i(i+1) 1..n中和m互质的 ...

  6. ACM-ICPC 2018 沈阳赛区网络预赛 J树分块

    J. Ka Chang Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero p ...

  7. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)

    ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...

  8. ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)

    https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...

  9. ACM-ICPC 2018 沈阳赛区网络预赛-K:Supreme Number

    Supreme Number A prime number (or a prime) is a natural number greater than 11 that cannot be formed ...

随机推荐

  1. 当springMVC 容器初始化完成后执行某个方法

    分类: spring java2013-06-19 16:40 8289人阅读 评论(4) 收藏 举报 在某些应用中,我们希望,当spring 容器将所有的bean都初始化完成后,做一个操作(例如:将 ...

  2. 3D分子构型该怎么优化

    很多的化学领域的专业人士都知道,分子的性质往往是有分子的结构所决定的,但是在实验室研究的过程中很难观察到稳定分子的结构,这往往是因为中间体寿命过短或者是混合物难以分离造成的,这个时候就需要通过计算化学 ...

  3. Unable to acquire Oracle environment handle 问题的解决

    ---恢复内容开始--- 转自:http://blog.csdn.net/zhangweiwindow/article/details/6575224 今天重装了一下系统,所以以前工作时配置的pyth ...

  4. Android之dip、dp、px、sp和屏幕密度

    1. dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这 这个,不依 ...

  5. 160307、Java调用Oracle存储过程返回结果集

    一:无返回值的存储过程调用 存储过程: CREATE OR REPLACE PROCEDURE PRO_1(PARA1 IN VARCHAR2,PARA2 IN VARCHAR2)   AS BEGI ...

  6. 160317(一)、在非action中获取request

    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes() ...

  7. nginx分发请求的2种方式:1、指明server_name;2、通过location过滤uri来分发请求;

    user nginx; worker_processes 8; # = cpu num; error_log /data/nginx/log/error/error.log warn; # warn, ...

  8. multi-paradigm

    w范式 https://developer.mozilla.org/en-US/docs/Web/JavaScript https://developer.mozilla.org/zh-CN/docs ...

  9. [已解决]centos6.4 php连接mysql和memcache提示权限不允许

    昨天新安装了64位系统,所以就重新安装了一个centos的虚拟机,配置了一个测试环境,刚开始是页面打不开,这个确定是selinux规则没有设置好使用如下命令解决: chcon -R -h -t htt ...

  10. centos 7.5 最小化安装

    参考:https://www.tecmint.com/centos-7-installation/ ================================================== ...