可推出$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. [openwrt]网络配置

    Network: config interface 'loopback'    option ifname 'lo'    option proto 'static'    option ipaddr ...

  2. python MySQLdb在windows环境下的快速安装

    python MySQLdb在windows环境下的快速安装.问题解决方式 使用python访问mysql,需要一系列安装 linux下MySQLdb安装见 Python MySQLdb在Linux下 ...

  3. Hive将txt、csv等文本文件导入hive表

    1.将txt文本文件放置hdfs目录下 2.登录hive并进入到指定数据库 3.创建表 create external table if not exists fun_user_external ( ...

  4. log4net写txt日志

    1.配置: <configSections>节点下添加: <section name="log4net" type="log4net.Config.Lo ...

  5. Dynamics CRM 2015 Update 1 系列(3): API的那些事 - Old APIs VS New APIs

    今天我们来看看API的变化.新系统中,去掉了一些经常使用的数据处理API,比如:SetStateRequest, SetBusinessUnitRequest, SetParentBusinessUn ...

  6. 《Shiro框架》shiro学习中报错解决方法

    [1] 最近在学习shiro,在学习过程中出现了一个问题,报错如下: org.apache.shiro.UnavailableSecurityManagerException: No Security ...

  7. poj_2823 线段树

    题目大意 给定一行数,共N个.有一个长度为K的窗口从左向右滑动,窗口中始终有K个数字,窗口每次滑动一个数字.求各个时刻窗口中的最大值和最小值. 题目分析 直接搜索,复杂度为O(n^2).本题可以看做是 ...

  8. c++ 利用容器vector动态的定义二维数组

    #include <iostream> #include <vector> using namespace std; int main() { int row, column; ...

  9. [SCOI2010]字符串

    思路: 设1为向(1,1)方向走,0为向(1,-1)方向走.那么题意可转化为从(0,0)走到(n+m,n-m)且不能跨过y=0的方案数.总方案数C(n+m,n),然后要减去不合法的即线路通过y=-1的 ...

  10. VC/MFC中通过CWebPage类调用javascript函数(给js函数传参,并取得返回值)

    转自:http://www.cnblogs.com/javaexam2/archive/2012/07/14/2632959.html ①需要一个别人写好的类CWebPage,将其对于的两个文件Web ...