wannafly27D(枚举计数+容斥)

首先gcd只能是x的因子,因此枚举x的因子计数

然后要判断gcd为d的个数有多少,设为f(d),而f(d)比较难计算,所以要考虑预处理

对每一个加进来的数,把他们的因子都分别记个数,得到g(d),为能被d整除的数的个数

那么这样就可以得到f(d)了,从大到小枚举x的因子,那么f(d)=g(d)-\sum_{d|k\&\&d>k}^{k|n}f(k)

然后对每个x的因子d,把d的因子对应的减去f(d)就可以了。。

然后还有就是gcd的个数其实比较有限,即无用状态其实很多的,对 f(d)==0的就不用枚举d的因子了,快了很多。。。

所以时间复杂度上限为O(nq^2)其中q为因子个数

 

 

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid ((x+y)/2)
#define NM 100015
#define nm 100005
#define pi 3.1415926535897931
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}






int n,m,t,c[NM],d[129],tot,b[205];
ll inf,ans;
ll qpow(ll x,int t){return t?qpow(sqr(x)%inf,t>>1)*(t&1?x:1)%inf:1LL;}
/*ll qpow(ll x,int k){
    ll res =1;
    while(k){
	if(k&1)res = res*x%inf;
	x = sqr(x)%inf;
    }
    return res;

}
*/
int main(){
    //freopen("data.in","r",stdin);
    m=read();
    while(m--){
	n=read();t=read();inf=read();
	ans=tot=0;
	for(int i=1;sqr(i)<n;i++)if(n%i==0)c[i]++,b[++tot]=i,d[tot]=c[i];
	for(int i=sqrt(n);i>=1;i--)if(n%i==0)c[n/i]++,b[++tot]=n/i,d[tot]=c[n/i];
	dec(i,tot,1)if(d[i]){
	    ans+=d[i]*qpow(b[i],t);ans%=inf;
	    inc(j,1,i-1)if(b[i]%b[j]==0)d[j]-=d[i];
	}
	printf("%lld\n",ans);
    }
    return 0;
}

 

 

 

 

 

链接:https://www.nowcoder.com/acm/contest/215/D
来源:牛客网
 

绿魔法师

时间限制:C/C++ 5秒,其他语言10秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

 

“我不知道你在说什么,因为我只是个pupil。”--绿魔法师

 

一个空的可重集合S。
n次操作,每次操作给出x,k,p,执行以下操作:
1、在S中加入x。
2、输出

输入描述:

所有输入的数都是小于1e5+1的正整数。

输出描述:

输出对应的结果

 

示例1

输入

复制

3
4 1 9
5 2 8
6 3 7

输出

复制

4
2
1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值