[ACM-ICPC 2018 沈阳网络赛] G. Spare Tire (思维+容斥)

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

SOLUTION:考虑容斥,用所有的和减去不合法的和也就是减去ai,其中i和m的gcd不为1考虑对m进行质因子分解对于m的每一质因子p我们需要减去小标为p的倍数的a写出来之后发现可以推出来式子o(1)的进行计算,但是俩个质因子p可能筛掉一个ai多次,加上容斥就行了

CODE:
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const ll mod = 1e9+7;
 5 const ll inv6 = 166666668;
 6 const ll inv2 = 500000004;
 7 ll a[10005];
 8 ll clac(ll n,ll i){
 9     n /= i;
10     return (n % mod * (n + 1) % mod * (2 * n + 1) % mod * inv6 % mod * i % mod * i % mod + n % mod * (n + 1) % mod * inv2 % mod * i % mod) % mod;
11 }
12 int main(){
13     ll n,m;
14     while(~scanf("%lld%lld",&n,&m)){
15         int cnt = 0;
16         for(int i = 2; i * i <= m; i++){
17             if(m % i == 0){
18                 a[cnt++] = i;
19                 while(m % i == 0)
20                     m /= i;
21             }
22         }
23         if(m != 1)
24             a[cnt++] = m;
25         ll ans = clac(n,1);
26         ll ans2 = 0;
27         for(int i = 1; i < (1 << cnt); i++){
28             int flag = 0;
29             ll tmp = 1;
30             for(int j = 0; j < cnt; j++){
31                 if(i & (1 << j)){
32                     flag++;
33                     tmp = tmp * a[j] % mod;
34                 }
35             }
36             tmp = clac(n,tmp);
37             if(flag & 1) ans2 = (ans2 % mod + tmp % mod) % mod;
38             else ans2 = (ans2 % mod - tmp % mod + mod) % mod;
39         }
40         printf("%lld\n",(ans % mod - ans2 % mod + mod) % mod);
41     }
42     return 0;
43 }



原文地址:https://www.cnblogs.com/zhangbuang/p/11172086.html

时间: 2024-07-30 03:07:12

[ACM-ICPC 2018 沈阳网络赛] G. Spare Tire (思维+容斥)的相关文章

2018沈阳网络赛G

容斥+状压 #include<bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 1e9+7; #include<vector> //const int MAX = 110; const int N = 100000; int arr[6000000];//p[N]用来存质数 ll n; int p; vector<ll>v; void getp(ll m, ll n) { /

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

这题很好啊,好在我没做出来...大概分析了一下,题目大概意思就是求 问所有满足1<=i<=n且i与m互素的ai之和 最开始我们队的做法是类似线性筛的方法去筛所有数,把数筛出来后剩下数即可,但是这样的是时间复杂度十分大,我们需要遍历每个质因 的倍数,这样最坏的复杂度是很大的1e8,因为我们需要把i的倍数筛到1e8,这样肯定不行,那么想想其他办法 我们想到了容斥-----(赛后想到的) 我们可以推处一个公式ai=i*i+i; 那么ai的前n项和Tn=n*(n+1)*(2*n+1)/6+n*(n+1

2014 ACM/ICPC 鞍山赛区网络赛(清华命题)

为迎接10月17号清华命题的鞍山现场赛 杭电上的题目 Biconnected(hdu4997)      Rotate(hdu4998)     Overt(hdu4999)   Clone(hdu5000)   Walk(hdu5001)   LianLianKan   Rescue   Spy's Work   Color the Tree   The Ghost Blows Light   USACO ORZ   2013/8/27

[ACM-ICPC 2018 沈阳网络赛] Ka Chang (dfs序+树状数组+分块)

Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero point. Then, you need to handle QQ operations. There're two types: 1\ L\ X1 L X: Increase points by XX of all nodes whose depth equals LL ( the depth of the root i

2018沈阳网络赛J

给一颗树,两种操作,一种把同一层的点权值加上v,另一种求一点下的子树权值和. 按层数中点个数分块,小块直接暴力把所有点用bit更新,大块把层的值存下来. 询问的时候子树权值和为bit中的值以及其下面的点在大块中的值,下面中的点在大块中的值用二分实现. #include <bits/stdc++.h> #include <unordered_set> #include <unordered_map> #define pb push_back #define mp make

icpc 2018 北京网络赛 A(搜索)

Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King S

ICPC 2018 南京网络赛 J Magical Girl Haze(多层图最短路)

传送门:https://nanti.jisuanke.com/t/A1958 题意:n个点m条边的路,你有k次机会将某条路上的边权变为0,问你最短路径长度 题解:最短路变形,我们需要在常规的最短路上多开 一维,记录,我消耗j次机会时的最短路径长度为多少 代码: /** * ┏┓ ┏┓ * ┏┛┗━━━━━━━┛┗━━━┓ * ┃ ┃ * ┃ ━ ┃ * ┃ > < ┃ * ┃ ┃ * ┃... ⌒ ... ┃ * ┃ ┃ * ┗━┓ ┏━┛ * ┃ ┃ Code is far away fro

2015沈阳网络赛1003 Minimum Cut 树链剖分 数组维护前缀和进行区间增减

2015沈阳网络赛1003  Minimum Cut   树链剖分 数组维护前缀和进行区间增减 Minimum Cut Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Given a simple unweighted graph G 

2018 CCPC网络赛

2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物品,问最终赚的钱的最大值. solution 用两个堆来维护,一个堆维护已经找到卖家的,一个堆维护还没找到卖家的. 对于第\(i\)个地点,在已经找到卖家的堆里找出卖的钱的最小值,如果最小值小于\(a_i\),则将卖家换成\(i\),然后将原来的卖家放到没找到卖家的那里:如果最小值对于\(a_i\)