沈阳网络赛G-Spare Tire【容斥】

  • 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了

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<algorithm>
  5 #include<stack>
  6 #include<queue>
  7 #include<map>
  8 #include<vector>
  9 #include<cmath>
 10 #include<cstring>
 11 #include<set>
 12 //#include<bits/stdc++.h>
 13 #define inf 0x7f7f7f7f7f7f7f7f
 14 using namespace std;
 15 typedef long long LL;
 16
 17 const int maxn = 1e5 + 5;
 18 const LL mod = 1e9 + 7;
 19 LL inv6, inv2;
 20 LL n, m;
 21
 22
 23 LL p[maxn];
 24 int cnt;
 25 void getprime(LL x)
 26 {
 27     cnt = 0;
 28     for (LL i = 2; i * i <= x; i++) {
 29         if (x % i == 0) {
 30             p[cnt++] = i;
 31         }
 32         while (x % i == 0) {
 33             x /= i;
 34         }
 35     }
 36     if (x > 1) {
 37         p[cnt++] = x;
 38     }
 39 }
 40
 41 void ex_gcd(int a, LL b, LL &d, LL &x, LL &y)
 42 {
 43     if(!b){
 44         d = a;
 45         x = 1;
 46         y = 0;
 47     }
 48     else{
 49         ex_gcd(b, a % b, d, y, x);
 50         y -= x * (a / b);
 51     }
 52 }
 53
 54 int mod_inverse(int a, LL m)
 55 {
 56     LL x, y, d;
 57     ex_gcd(a, m, d, x, y);
 58     return (m + x % m) % m;
 59 }
 60
 61 LL cal(LL n, LL k)
 62 {
 63     /*LL ans = n / k * (n / k + 1) % mod;
 64     ans = ans * (2 * n / k + 1) % mod;
 65     ans = ans * inv6 % mod * k % mod * k % mod;
 66     ans = ans + k * (1 + n / k) % mod * n / k % mod * inv2 % mod;*/
 67     n=n/k;
 68     return (n%mod*(n+1)%mod*(2*n+1)%mod*inv6%mod*k%mod*k%mod+n%mod*(n+1)%mod*inv2%mod*k%mod)%mod;
 69     //return ans;
 70 }
 71
 72
 73 int main()
 74 {
 75     inv6 = mod_inverse(6, mod);
 76     inv2 = mod_inverse(2, mod);
 77     //cout<<mod_inverse(6, mod)<<endl<<mod_inverse(2, mod)<<endl;
 78     while (scanf("%lld%lld", &n, &m) != EOF) {
 79         memset(p, 0, sizeof(p));
 80         getprime(m);
 81         LL ans = 0;
 82         for(int i = 1; i < (1 << cnt); i++){
 83             int flag = 0;
 84             LL tmp = 1;
 85             for(int j = 0; j < cnt; j++){
 86                 if(i & (1 << j)){
 87                     flag++;
 88                     tmp = tmp * p[j] % mod;
 89                 }
 90
 91             }
 92             tmp = cal(n, tmp) % mod;
 93             if(flag % 2){
 94                 ans = (ans % mod + tmp % mod) % mod;
 95             }
 96             else{
 97                 ans = (ans % mod - tmp % mod + mod) % mod;
 98             }
 99         }
100         printf("%lld\n", (cal(n, 1) % mod - ans % mod + mod) % mod);
101     }
102     return 0;
103 }

原文地址:https://www.cnblogs.com/wyboooo/p/9646334.html

时间: 2024-07-30 20:32:39

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

[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? 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

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) { /

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 

2019ACM-ICPC沈阳网络赛-C-Dawn-K&#39;s water(完全背包模板题)

Dawn-K's water  1000ms 262144K Dawn-K recently discovered a very magical phenomenon in the supermarket of Northeastern University: The large package is not necessarily more expensive than the small package. On this day, Dawn-K came to the supermarket

2019ACM-ICPC沈阳网络赛-K-Guanguan&#39;s Happy water(思维+暴力)

Guanguan's Happy water 4000ms 262144K Rather than drinking happy water, Guanguan loves storing happy water. So he bought a refrigerator and stored a_iai? bottles of cola into it every day. When the storage is finished on the kk-th day, the refrigerat

2015长春、沈阳网络赛总结

我所说的总结并不是谈什么题目解法之类的东西 这些东西网上有很多题解 说说这两场网赛吧! 这两场我的状态还行,只是xiaodong还没有找到状态,长春赛lucas+中国剩余定理他硬是打了一整场,还是没打出来,版题没打出来确实不该 wzb状态一般,不过看题的能力依然那么厉害 长春赛中,很遗憾的只出了5道题,按当时过题数目,应该是7道德,可是小东的lucas那题没打出来,而我打得后缀数组那题,当顺时针的时候不用看是否是下标最前面的,但是反过来就需要看了,当时想当然的认为不用,交了4发,一直wa到比赛结

hdu5072 Coprime 2014鞍山现场赛C题 计数+容斥

http://acm.hdu.edu.cn/showproblem.php?pid=5072 Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 354    Accepted Submission(s): 154 Problem Description There are n people standing in a

HDU 6200 2017沈阳网络赛 树上区间更新,求和

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6200 题意:给个图,有2种操作,一种是加一条无向边,二是查询u,v之间必须有的边的条数,所谓必须有的边就是对于u,v必须通过这条边才能到达. 解法:一个很简单的想法,搞出图上的一颗树,然后剩下的边当成询问点队加到更新点集,每加入一个更新点对,直接把u,v区间的值置为0即可,查询就直接区间求和,可以直接树剖来维护,简单暴力,读入挂卡过.还有1个log的做法,可以用LCT维护(这个没写,口胡的) #in