• 17.64%
  • 1000ms
  • 131072K
 

A sequence of integer \lbrace a_n \rbrace{an​} can be expressed as:

\displaystyle a_n = \left\{ \begin{array}{lr} 0, & n=0\\ 2, & n=1\\ \frac{3a_{n-1}-a_{n-2}}{2}+n+1, & n>1 \end{array} \right.an​=⎩⎨⎧​0,2,23an−1​−an−2​​+n+1,​n=0n=1n>1​

Now there are two integers nn and mm. I'm a pretty girl. I want to find all b_1,b_2,b_3\cdots b_pb1​,b2​,b3​⋯bp​ that 1\leq b_i \leq n1≤bi​≤n and b_ibi​is relatively-prime with the integer mm. And then calculate:

\displaystyle \sum_{i=1}^{p}a_{b_i}i=1∑p​abi​​

But I have no time to solve this problem because I am going to date my boyfriend soon. So can you help me?

Input

Input contains multiple test cases ( about 1500015000 ). Each case contains two integers nn and mm. 1\leq n,m \leq 10^81≤n,m≤108.

Output

For each test case, print the answer of my question(after mod 1,000,000,0071,000,000,007).

Hint

In the all integers from 11 to 44, 11 and 33 is relatively-prime with the integer 44. So the answer is a_1+a_3=14a1​+a3​=14.

样例输入复制

4 4

样例输出复制

14

题目来源

ACM-ICPC 2018 沈阳赛区网络预赛

题意:

已知一个数列a 和整数n, m

现在想知道1-n中所有与m互质的数 作为下标的a的和

思路:

预先打表处理的话内存不够 推公式能推出a[i] = (i + 1) * i【虽然我好像没有推出来...】

所以Sn也是可以推出来的

某一个数的倍数的a求和也是可以推的 因为是ki 那么提出一个k来 就可以代入公式求了

小于n 与m互质的数没有什么特殊的规律 但应该想到素数筛时的做法 用上容斥

先求出1-n所有的a的和 再减去所有与m不互质的数

用容斥来求不互质的数 是正是负与质因数的个数有关 如果是奇数个质因数之积的话就是加 偶数就是减

用到了乘法逆元的模板

cal()函数注释掉的部分是WA的 改成了题解的方法就AC了

 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<cmath>
#include<cstring>
#include<set>
//#include<bits/stdc++.h>
#define inf 0x7f7f7f7f7f7f7f7f
using namespace std;
typedef long long LL; const int maxn = 1e5 + ;
const LL mod = 1e9 + ;
LL inv6, inv2;
LL n, m; LL p[maxn];
int cnt;
void getprime(LL x)
{
cnt = ;
for (LL i = ; i * i <= x; i++) {
if (x % i == ) {
p[cnt++] = i;
}
while (x % i == ) {
x /= i;
}
}
if (x > ) {
p[cnt++] = x;
}
} void ex_gcd(int a, LL b, LL &d, LL &x, LL &y)
{
if(!b){
d = a;
x = ;
y = ;
}
else{
ex_gcd(b, a % b, d, y, x);
y -= x * (a / b);
}
} int mod_inverse(int a, LL m)
{
LL x, y, d;
ex_gcd(a, m, d, x, y);
return (m + x % m) % m;
} LL cal(LL n, LL k)
{
/*LL ans = n / k * (n / k + 1) % mod;
ans = ans * (2 * n / k + 1) % mod;
ans = ans * inv6 % mod * k % mod * k % mod;
ans = ans + k * (1 + n / k) % mod * n / k % mod * inv2 % mod;*/
n=n/k;
return (n%mod*(n+)%mod*(*n+)%mod*inv6%mod*k%mod*k%mod+n%mod*(n+)%mod*inv2%mod*k%mod)%mod;
//return ans;
} int main()
{
inv6 = mod_inverse(, mod);
inv2 = mod_inverse(, mod);
//cout<<mod_inverse(6, mod)<<endl<<mod_inverse(2, mod)<<endl;
while (scanf("%lld%lld", &n, &m) != EOF) {
memset(p, , sizeof(p));
getprime(m);
LL ans = ;
for(int i = ; i < ( << cnt); i++){
int flag = ;
LL tmp = ;
for(int j = ; j < cnt; j++){
if(i & ( << j)){
flag++;
tmp = tmp * p[j] % mod;
} }
tmp = cal(n, tmp) % mod;
if(flag % ){
ans = (ans % mod + tmp % mod) % mod;
}
else{
ans = (ans % mod - tmp % mod + mod) % mod;
}
}
printf("%lld\n", (cal(n, ) % mod - ans % mod + mod) % mod);
}
return ;
}

沈阳网络赛G-Spare Tire【容斥】的更多相关文章

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

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

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

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

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

    可推出$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} ...

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

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

  5. 2018 ICPC 沈阳网络赛

    2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...

  6. 沈阳网络赛 F - 上下界网络流

    "Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...

  7. 15ecjtu校赛1006 (dfs容斥)

    Problem Description 在平面上有一个n*n的网格,即有n条平行于x轴的直线和n条平行于y轴的直线,形 成了n*n个交点(a,b)(1<=a<=n,1<=b<= ...

  8. 【2019沈阳网络赛】G、Special necklace——自闭的物理题

    这道题让我差点怀疑自己高考没考过物理 题意中 he measures the resistance of any two endpoints of it, the resistance values ...

  9. 沈阳网络赛D-Made In Heaven【k短路】【模板】

    One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. However, Pucci ...

随机推荐

  1. 您可以从 Windows 命令行上运行 gcc、g++、ar、ranlib、dlltool 和其他一些 GNU 工具

    Windows 上的安装为了在 Windows 上安装 GCC,您需要安装 MinGW.为了安装 MinGW,请访问 MinGW 的主页 www.mingw.org,进入 MinGW 下载页面,下载最 ...

  2. (转)SDL 1.2 to 2.0 Migration Guide--SDL1.2更新到SDL2.0指南

    SDL 1.2 to 2.0 Migration Guide 目录 SDL 1.2 to 2.0 Migration Guide Translations Introduction Overview ...

  3. LINUX下CPU Load Average的一点研究

    背景: 公司的某个系统工作在基于Linux的Cent OS下,一个host下同时连接了许多client, 最近某台Host总是显示CPU Load Average过高,我们单纯的以为是CPU的占用过高 ...

  4. Java线程之Callable和Future

    本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果.        Callable接口类似于Runnable,从名字就可以看出来了,但是Runnable不会返回结 ...

  5. android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)

    这个特性是andorid4.4支持的,最少要api19才干够使用.以下介绍一下使用的方法,很得简单: 添加一个demo源代码: https://github.com/ws123/StatusDemo ...

  6. UVA 11542 - Square(高斯消元)

    UVA 11542 - Square 题目链接 题意:给定一些数字.保证这些数字质因子不会超过500,求这些数字中选出几个,乘积为全然平方数,问有几种选法 思路:对每一个数字分解成质因子后.发现假设要 ...

  7. socket网络编程【python】

    转自:http://www.jb51.net/article/19751.htm socket 是网络连接端点. 一.网络知识的一些介绍 socket 是网络连接端点.例如当你的Web浏览器请求www ...

  8. Unity随机Prefab,自动前往某点处理

    对与U3D  AI,看了下,自己做了小功能,以备后用啊! 一,在某区域随机产生某个对象 C# 文件名称为RadomAPoint.cs using UnityEngine; using System.C ...

  9. PHP虚拟主机的配置

    今天配置了一下虚拟目录,以下是我的配置方法. 1  编辑httpd.conf,查找Include conf/extra/httpd-vhosts.conf,把前面注释符号“#”删掉. 2  编辑htt ...

  10. PyQt4预定义对话框

    PyQt4中的对话框 对话窗口和对话框是现代GUI应用程序必不可少的一部分.生活中“对话”被定义为发生在两人或更多人之间的会话.而在计算机世界,“对话”则时人与应用程序之间的“会话”.人及对话的形式有 ...