Bzoj3481 DZY Loves Math III

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 310  Solved: 65

Description

Input

Output

Sample Input

3
1
2
3
2
4
2

Sample Output

6

HINT

1<=N<=10,0<=Qi<=10^18,1<=Pi<=10^18,P>=2

本题仅四组数据。

Source

By Jc

数学问题 欧拉函数 Miller-Rabin Pollard-rho

花了一整晚来怼这道题……在long long的领域遇到了许多问题。

假设我们有充足的时间枚举每一个x,那么在x确定的情况下,原式变成了一个模方程。
根据裴蜀定理,我们知道只有当$ gcd(x,P)|Q $ 的情况下方程有 $ gcd(x,P) $ 个解。
现在我们可以愉快地枚举每一个gcd,那么答案就是:
$$ans=\sum_{d|P,d|Q} d * \sum_{x}[gcd(x,\frac{P}{d})==1]$$
后面那个sum显然是欧拉函数
那么$$ ans=\sum_{d|P,d|Q} d · \varphi(\frac{P}{d}) $$
分(an)析(zhao)一(tao)波(lu),发现这是一个积性函数,所以我们可以分别考虑每个质因子的贡献,再把它们累乘起来。
我们现在考虑一个质因子p,它在P中有$q$个,在Q中有$q‘$个:
它对答案的贡献是
$$ \sum_{i=0}^{q‘} p^i * \varphi(p^{q-i})$$
我们知道
$$\varphi(p^{q})=p^{q}·\frac{p-1}{p}$$
所以上面的sum可以化成:
$$p^{q-1}·(p-1)·(q‘+1)$$
但是有一个特殊情况,当i=q的时候,$\varphi(1)=1$,不能表示成$\frac{p-1}{p}$的形式,而我们却把它用这种形式算进去了。
也就是说我们把一个$p^q$ 的贡献算成了 $(p-1)p^{q-1}$,特判一下消除即可。

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstring>
  4 #include<cstdio>
  5 #include<cmath>
  6 #include<map>
  7 #define LL long long
  8 using namespace std;
  9 const int mxn=100010;
 10 LL read(){
 11     LL x=0,f=1;char ch=getchar();
 12     while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
 13     while(ch>=‘0‘ && ch<=‘9‘){x=x*10-‘0‘+ch;ch=getchar();}
 14     return x*f;
 15 }
 16 LL Rand(LL n){return (((LL)rand()<<31)|rand())%(n-1)+1;}
 17 //
 18 map<LL,int>primap;
 19 LL pri[mxn*10];int cnt=0;
 20 bool vis[mxn];
 21 void init(){
 22     int mxn=200;
 23     for(int i=2;i<mxn;i++){
 24         if(!vis[i]){
 25             pri[++cnt]=i;
 26             primap[i]=cnt;
 27         }
 28         for(int j=1;j<=cnt && pri[j]*i<mxn;j++){
 29             vis[pri[j]*i]=1;
 30             if(i%pri[j]==0)break;
 31         }
 32     }
 33     return;
 34 }
 35 //
 36 LL mul(LL x,LL k,LL P)
 37 {
 38     LL res=0;
 39     while(k){
 40         if(k&1)res=(res+x)%P;
 41         x=(x+x)%P;
 42         k>>=1;
 43     }
 44     return res;
 45 }
 46 /*
 47 LL mul(LL a,LL b,LL P){
 48     LL d=(long double)a/P*b+1e-8;
 49
 50     a=a*b-d*P;
 51     a=(a<0)?a+P:a;
 52     printf("%lld \n",a);
 53     return a;
 54 }*/
 55 LL ksm(LL a,LL k,LL P){
 56     LL res=1;
 57     while(k){
 58         if(k&1)res=mul(res,a,P);
 59         a=mul(a,a,P);
 60         k>>=1;
 61     }
 62     return res;
 63 }
 64 ///
 65 bool check(LL a,LL n,LL r,LL s){
 66     LL res=ksm(a,r,n);LL b=res;
 67     for(int i=1;i<=s;i++){
 68         res=mul(res,res,n);
 69         if(res==1 && b!=1 && b!=n-1)return 1;
 70         b=res;
 71     }
 72     return (res!=1);
 73 }
 74 bool MR(LL n){//素数测试
 75     if(n<=1)return 0;
 76     if(n==2)return 1;
 77     if(~n&1)return 0;
 78     LL r=n-1,s=0;
 79     while(!(r&1))r>>=1,s++;
 80     for(int i=1;i<=10;i++)
 81         if(check(Rand(n),n,r,s))return 0;
 82     return 1;
 83 }
 84 ///
 85 LL p[mxn],q[mxn];
 86 void addpri_P(LL x){
 87     if(primap.count(x)){
 88         ++p[primap[x]];return;
 89     }
 90     pri[++cnt]=x;
 91     primap[x]=cnt;
 92     p[cnt]=1;
 93     return;
 94 }
 95 void addpri_Q(LL x){
 96     if(primap.count(x)){
 97         int t=primap[x];
 98         if(q[t]<p[t])++q[t];
 99     }
100     return;
101 }
102 LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
103 LL Rho(LL n,LL c){
104     if(n<0)while(1);
105     LL k=2,x=Rand(n),y=x,p=1;
106     for(LL i=1;p==1;i++){
107         x=(mul(x,x,n)+c)%n;
108         p=(y>x)?(y-x):(x-y);
109         p=gcd(n,p);
110         if(i==k)y=x,k<<=1;
111     }
112     return p;
113 }
114 void solve(LL x,bool flag){//分解质因数
115     if(x==1)return;
116     if(MR(x)){
117         if(!flag)addpri_P(x);//P
118         else addpri_Q(x);//Q
119         return;
120     }
121     LL t=x;
122     while(t==x)t=Rho(x,Rand(x));
123     solve(t,flag);
124     solve(x/t,flag);
125     return;
126 }
127 //
128 const int mod=1e9+7;
129 int n;
130 LL P[50],Q[50];
131 void Calc(){
132     LL ans=1;
133     for(int i=1;i<=cnt;i++){
134         if(!p[i])continue;
135         LL R=mul((q[i]+1),(pri[i]-1),mod);
136         if(p[i]==q[i])R++;
137         R=mul(R,ksm(pri[i],p[i]-1,mod),mod);
138         ans=mul(ans,R,mod);
139     }
140     printf("%lld\n",ans);
141     return;
142 }
143 int main(){
144     srand(19260817);
145     init();
146     int i,j;
147     n=read();
148     for(i=1;i<=n;i++){
149         P[i]=read();
150         solve(P[i],0);
151     }
152     for(i=1;i<=n;i++){
153         Q[i]=read();
154         if(!Q[i]){//特判0
155             for(j=1;j<=cnt;j++)q[j]=p[j];
156             break;
157         }
158         else solve(Q[i],1);
159     }
160     Calc();
161     return 0;
162 }
时间: 2024-08-01 10:46:43

Bzoj3481 DZY Loves Math III的相关文章

【BZOJ】3309: DZY Loves Math 莫比乌斯反演优化

3309: DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, f(1)=0. 给定正整数a,b,求sigma(sigma(f(gcd(i,j)))) (i=1..a, j=1..b). Input 第一行一个数T,表示询问数. 接下来T行,每行两个数a,b,表示一个询问. Output 对于每一个询问,输出一行一个非负整数作为回答. Sample In

[BZOJ3568]DZY Loves Math VII

本人BZOJ的处女作. 这题题面还是蛮有趣的吧. 然后三个问题都蛮有意思的. 要保证正确性,出数据还是异常蛋疼啊. 本来各出三题的.但是考虑到是OJ上的题,就搞在一起了.这样代码量就会比较大. [BZOJ3568]DZY Loves Math VII,布布扣,bubuko.com

【BZOJ 3560】 3560: DZY Loves Math V (欧拉函数)

3560: DZY Loves Math V Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 241  Solved: 133 Description 给定n个正整数a1,a2,-,an,求 的值(答案模10^9+7). Input 第一行一个正整数n. 接下来n行,每行一个正整数,分别为a1,a2,-,an. Output 仅一行答案. Sample Input 3 6 10 15 Sample Output 1595 HINT 1<=n<=1

bzoj 3309 DZY Loves Math

LINK:DZY Loves Math 一道比较有意思的数论题 原谅我的智障多调了40min. 可以简单的推式子推出 答案为\(\sum{w=1}^n\frac{n}{w}\frac{m}{w}\sum{x|w}\mu(x)f(\frac{w}{x})\) f函数定义和题目中一致. 考虑后面前缀和怎么求 发现光求f(x)复杂度都比较高.如果我们把f(x)求出再调和级数预处理 那得GG 1e7过不了log+根号 考虑考虑一下\(\mu\)和f的这种形式肯定值有局限 设后面的东西为g(x) 不难发现

bzoj 3309 DZY Loves Math - 莫比乌斯反演 - 线性筛

对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, f(1)=0. 给定正整数a,b,求sigma(sigma(f(gcd(i,j)))) (i=1..a, j=1..b). Input 第一行一个数T,表示询问数. 接下来T行,每行两个数a,b,表示一个询问. Output 对于每一个询问,输出一行一个非负整数作为回答. Sample Input 4 7558588 9653114 6514903 445

bzoj 3560 DZY Loves Math V - 线性筛 - 数论 - 扩展欧几里得算法

给定n个正整数a1,a2,…,an,求 的值(答案模10^9+7). Input 第一行一个正整数n. 接下来n行,每行一个正整数,分别为a1,a2,…,an. Output 仅一行答案. Sample Input 3 6 10 15 Sample Output 1595 Hint 1<=n<=10^5,1<=ai<=10^7.共3组数据. 题目大意 (题目过于简洁,完全不需要大意) 题目虽然很简洁,但是处处挖着坑等你跳. 原计划两个小时把今天讲的例题A完,实际上两个小时都被这道题

BZOJ3560: DZY Loves Math V

虽然不是很神的题,不过拿短代码搞到rank1了那么纪念下. 先观察样例. 6=2*3; 10=2*5; 15=3*5; 对应答案:1595=5*11*29: 其中: 5=((1+2)*(1+2)*1*(2-1)+1)/2 11=((1+3)*1*(1+3)*(3-1)+1)/3 29=(1*(1+5)*(1+5)*(5-1)+1)/5 证明不写了……自行领会精神. 就是对每个n因数分解后对每个p分开搞搞. [代码](话说好不容易才发现插入代码这功能) 1 #include<cstdio> 2

【莫比乌斯反演】BZOJ3309 DZY Loves Math

Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, f(1)=0. 给定正整数a,b,求sigma(sigma(f(gcd(i,j)))) (i=1..a, j=1..b).T<=1e4; a,b<=1e7. Solution 一开始没仔细看数据范围然后打了一个每个询问O(n)的,当然T了 (盗一张图) 一开始我按照第二行的做的,里层外层循环都和ab有关,每一层都要sqrt(n)

BZOJ3739 : DZY loves math VIII

显然当且仅当$\gcd(i,j)=1$时才对答案有贡献,化简得 \[\begin{eqnarray*}ans&=&\sum_{i=1}^n\sum_{j=1}^i\mu(ij)[\gcd(i,j)=1]\\ &=&\sum_{i=1}^n\sum_{j=1}^i\mu(ij)\sum_{d|i,d|j}\mu(d)\\ &=&\sum_{i=1}^n\sum_{d|i}\mu(d)\sum_{j=1}^{\frac{i}{d}}\mu(dj)\end{eqn