Educational Codeforces Round 48 (Rated for Div. 2)G. Appropriate Team

题意:求满足条件的(i,j)对数:\(gcd(v,a_i)=x,lcm(v,a_j)=y\)
题解:\(x|a_i,a_j|y\),\(x|y\),考虑质因子p,假设a_i中p次数为a,x中次数为b,y为c,\(a_j\)为d;a>=b,c>=d.
假设a>b,c>d,那么由于\(gcd(v,a_i)=x\),v中p的次数为b,由于\(lcm(v,a_j)=y\),那么\(max(b,d)==c\),又c>d,所以b=c<a和x|y矛盾,所以此时ij不满足条件
其他情况同理,能证明当a>b,c>d不同时满足时,都能,满足条件,考虑y的质因子只有15个,二进制状压,表示1为a>b,0为a==b,那么当两个二进制数and起来为0时,ij对满足条件.
分解质因子用泼辣的肉,and用fwt或者sosdp都行

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000009
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
inline ll qm(ll a,ll b,ll c){ll ans=0;while(b){if(b&1)ans=(ans+a)%c;a=(a+a)%c;b>>=1;}return ans%c;}
inline ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=qm(ans,a,c)%c;a=qm(a,a,c)%c;b>>=1;}return ans;}

using namespace std;

const ull ba=233;
const db eps=1e-10;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=200000+10,maxn=100000+10,inf=0x3f3f3f3f;

int cnt;
ll f[110];
bool check(ll a,ll n,ll x,ll sum){
    ll judge=qpow(a,x,n);
    if (judge==n-1||judge==1)return 1;
    while (sum--){
        judge=qm(judge,judge,n);
        if (judge==n-1)return 1;
    }
    return 0;
}
bool miller(ll n){
    if (n<2)return 0;
    if (n==2)return 1;
    if ((n&1)==0)return 0;
    ll x=n-1,sum=0;
    while (x%2==0)x>>=1,sum++;
    for (ll i=1;i<=20;i++){
        ll a=rand()%(n-1)+1;
        if (!check(a,n,x,sum))return 0;
    }
    return 1;
}
ll pollard(ll n,ll c){
    ll x,y,d,i=1,k=2;
    x=rand()%n;y=x;
    while (1){
        i++;
        x=(qm(x,x,n)+c)%n;
        d=gcd(y-x,n);
        if (d<0)d=-d;
        if (d>1&&d<n)return d;
        if (y==x)return n;
        if (i==k)y=x,k<<=1;
    }
}
void Find(ll n){
    if(n==1)return;
    if (miller(n)){
        f[cnt++]=n;
        return ;
    }
    ll p=n;
    while (p>=n) p=pollard(p,rand()%(n-1)+1);
    Find(n/p);Find(p);
}
ll a[N],b[N],c[N];
int cal(ll x,ll y)
{
    int ans=0;
    while(x%y==0)ans++,x/=y;
    return ans;
}
void fwt_and(ll *a,int n,int dft)
{
    for(int i=1;i<n;i<<=1)
        for(int j=0;j<n;j+=i<<1)
            for(int k=j;k<j+i;k++)
            {
                if(dft==1)a[k]=a[k]+a[i+k];
                else a[k]=a[k]-a[i+k];
            }
}
int main()
{
    int n;ll x,y;scanf("%d%lld%lld",&n,&x,&y);
    if(y%x)return 0*puts("0");
    Find(y);
    sort(f,f+cnt);cnt=unique(f,f+cnt)-f;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        int p1=0,p2=0;
        for(int j=0;j<cnt;j++)if(cal(x,f[j])!=cal(y,f[j]))
        {
            p1+=(1<<j)*(cal(a[i],f[j])>cal(x,f[j]));
            p2+=(1<<j)*(cal(a[i],f[j])<cal(y,f[j]));
        }
        if(a[i]%x==0)b[p1]++;
        if(y%a[i]==0)c[p2]++;
    }
    fwt_and(b,(1<<cnt),1);fwt_and(c,(1<<cnt),1);
    for(int i=0;i<(1<<cnt);i++)b[i]=b[i]*c[i];
    fwt_and(b,(1<<cnt),-1);
    printf("%lld\n",b[0]);
    return 0;
}
/********************

********************/
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000009
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
inline ll qm(ll a,ll b,ll c){ll ans=0;while(b){if(b&1)ans=(ans+a)%c;a=(a+a)%c;b>>=1;}return ans%c;}
inline ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=qm(ans,a,c)%c;a=qm(a,a,c)%c;b>>=1;}return ans;}

using namespace std;

const ull ba=233;
const db eps=1e-10;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=200000+10,maxn=100000+10,inf=0x3f3f3f3f;

int cnt;
ll f[110];
bool check(ll a,ll n,ll x,ll sum){
    ll judge=qpow(a,x,n);
    if (judge==n-1||judge==1)return 1;
    while (sum--){
        judge=qm(judge,judge,n);
        if (judge==n-1)return 1;
    }
    return 0;
}
bool miller(ll n){
    if (n<2)return 0;
    if (n==2)return 1;
    if ((n&1)==0)return 0;
    ll x=n-1,sum=0;
    while (x%2==0)x>>=1,sum++;
    for (ll i=1;i<=20;i++){
        ll a=rand()%(n-1)+1;
        if (!check(a,n,x,sum))return 0;
    }
    return 1;
}
ll pollard(ll n,ll c){
    ll x,y,d,i=1,k=2;
    x=rand()%n;y=x;
    while (1){
        i++;
        x=(qm(x,x,n)+c)%n;
        d=gcd(y-x,n);
        if (d<0)d=-d;
        if (d>1&&d<n)return d;
        if (y==x)return n;
        if (i==k)y=x,k<<=1;
    }
}
void Find(ll n){
    if(n==1)return;
    if (miller(n)){
        f[cnt++]=n;
        return ;
    }
    ll p=n;
    while (p>=n) p=pollard(p,rand()%(n-1)+1);
    Find(n/p);Find(p);
}
ll a[N];
int b[N],c[N];
int cal(ll x,ll y)
{
    int ans=0;
    while(x%y==0)ans++,x/=y;
    return ans;
}
int main()
{
    int n;ll x,y;scanf("%d%lld%lld",&n,&x,&y);
    if(y%x)return 0*puts("0");
    Find(y);
    sort(f,f+cnt);cnt=unique(f,f+cnt)-f;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        int p1=0,p2=0;
        for(int j=0;j<cnt;j++)if(cal(x,f[j])!=cal(y,f[j]))
        {
            p1+=(1<<j)*(cal(a[i],f[j])>cal(x,f[j]));
            p2+=(1<<j)*(cal(a[i],f[j])<cal(y,f[j]));
        }
        if(a[i]%x==0)b[p1]++;//,printf("%d %d\n",i,p1);
        c[i]=p2;
    }
    for(int i=0;i<cnt;i++)for(int j=0;j<(1<<cnt);j++)
        if(j&(1<<i))b[j]+=b[j^(1<<i)];
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        if(y%a[i]!=0)continue;
        ans+=b[((1<<cnt)-1)^c[i]];
//        printf("%d %d\n",((1<<cnt)-1)^c[i],b[((1<<cnt)-1)^c[i]]);
    }
    printf("%lld\n",ans);
    return 0;
}
/********************

********************/

原文地址:https://www.cnblogs.com/acjiumeng/p/10505644.html

时间: 2024-11-05 22:42:31

Educational Codeforces Round 48 (Rated for Div. 2)G. Appropriate Team的相关文章

Educational Codeforces Round 48 (Rated for Div. 2)

http://codeforces.com/contest/1016 A. 没想到这个也会TLE,太粗心了 B. 暴力就好了,多情况讨论又出错... 思路跟我一样的解法   为什么我做了那么多讨论,原因是没注意这个: 标记 最后一个字符,同时注意 l+m-3. 特殊情况就 vis 0000011111111111112 s1  abaccabaacabacabacca      红色的地方是 l 和 r ,为了防止在 l 处计数多了就得 l + m - 3 s2  abacca we[s+m-1

Educational Codeforces Round 48 (Rated for Div. 2) - 赛后补题

C. Vasya And The Mushrooms 题解:拿笔画他的行走路线,你会发现可以前缀和预处理一些东西出来. const int N = 300005; int n; ll a[N], b[N], dp[2][N], sp[2][N], sum[N]; int get_a() { dp[0][0] = 0; for (int i = 1; i < n; ++i) { dp[0][i] = dp[0][i - 1] + 1ll * i * a[i]; } int t = n; dp[1]

Educational Codeforces Round 48 (Rated for Div. 2) B Segment Occurrences

翻译 给你一个字符串\(s\)和另一个字符串\(t\),然后给你\(q\)个区间,问\(s\)在这些区间里的子串有多少个与\(t\)相同. 思路 一道要细心的模拟题,使用\(STL string\),暴力,前缀和,\(Hash\),\(Kmp\)都能做出来,然后我来介绍一下用 \(vector\)的做法. 首先预处理\(s\),从头到位找到每一个长度是字符串t的长度\(m\)的字符串,如果其与\(t\)相等,那么就往vector中压入\(1\),否则压入\(0\),这样,我们就找到每个编号的字符

Educational Codeforces Round 41 (Rated for Div. 2) G. Partitions

G. Partitions time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a set of n elements indexed from 1 to n. The weight of i-th element is wi. The weight of some subset of a given

Educational Codeforces Round 47 (Rated for Div. 2)G. Allowed Letters 网络流

题意:给你一个字符串,和每个位置可能的字符(没有就可以放任意字符)要求一个排列使得每个位置的字符在可能的字符中,求字典序最小的那个 题解:很容易判断有没有解,建6个点表示从a-f,和源点连边,容量为原串字符出现次数,再建64个点表示给定的位置的每一个状态,和汇点连边,容量为出现次数,如果a-f某个字符在状态中出现过,再把a-f和状态连边,容量inf,但是这只能求可行解,并不是字典序最小, 我们枚举每个位置该放哪个字符(a-f),假设该位置是pos,枚举的字符是x,该位置可能字符的状态是st,现在

Educational Codeforces Round 36 (Rated for Div. 2) G. Coprime Arrays

求a_i 在 [1,k]范围内,gcd(a_1,a_2...,a_n) = 1的a的数组个数. F(x)表示gcd(a_1,a_2,...,a_n) = i的a的个数 f(x)表示gcd(a_1,a_2,...,a_n) = ki的a的个数(实际上就是i的倍数) f(x) = segma(x | d) F(d) F(x) = segma(x | d) mu(d / x) * f(d) F(1) = segma(d,1,k) mu(d) * f(d) f(d) = (k / d)^n 由于k变化时

Educational Codeforces Round 61 (Rated for Div. 2) G(线段树,单调栈)

#include<bits/stdc++.h>using namespace std;int st[1000007];int top;int s[1000007],t[1000007];int mx[4000007];int sum[4000007];int head[1000007],to[2000007],nex[2000007];int n,k;int a[10000077];int dfn;int tot;void pushup(int rt){    mx[rt]=max(mx[rt

Educational Codeforces Round 53 (Rated for Div. 2)G. Yet Another LCP Problem

题意:给串s,每次询问k个数a,l个数b,问a和b作为后缀的lcp的综合 题解:和bzoj3879类似,反向sam日神仙...lcp就是fail树上的lca.把点抠出来建虚树,然后在上面dp即可.(感觉之前写的svt什么玩意) //#pragma GCC optimize(2) //#pragma GCC optimize(3) //#pragma GCC optimize(4) //#pragma GCC optimize("unroll-loops") //#pragma comm

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ