当前位置:网站首页>Wc2020 guessing game
Wc2020 guessing game
2022-06-11 07:30:00 【Master. Yi】
Title Description
Topic analysis
If exist k k k bring a k ≡ b ( m o d p ) a^k\equiv b\pmod p ak≡b(modp), be a a a towards b b b One side .
Then the condition for a point to be selected in a set is that no other point in the set can reach it ( Shrink the ring to a point , The number of schemes to be selected for each ring is 2 Ring Big Small − 1 2^{ Ring size }-1 2 Ring Big Small −1), Then the sum of points selected for all schemes is ∑ some individual Ring 2 n − ( can To reach this individual Ring Of spot Count ) ∗ ( 2 Ring Big Small − 1 ) \sum_{ A ring } 2^{n-( The number of points that can reach this ring )}*(2^{ Ring size }-1) ∑ some individual Ring 2n−( can To reach this individual Ring Of spot Count )∗(2 Ring Big Small −1)
The violence is O ( n p ) O(np) O(np) Of , It is easy to think of using primitive root optimization .
If p p p It's an odd prime , Then there is k k k bring g a k ≡ g b ( m o d p ) g^{ak}\equiv g^b\pmod p gak≡gb(modp) Equivalent to being k k k bring a k ≡ b ( m o d φ ( p ) ) ak\equiv b\pmod {\varphi(p)} ak≡b(modφ(p)), namely g c d ( a , φ ( p ) ) ∣ b gcd(a,\varphi(p))~|~b gcd(a,φ(p)) ∣ b .
use BSGS Index seeking , The complexity is O ( n p + n 2 ) O(n\sqrt p+n^2) O(np+n2).
When p = q k p=q^k p=qk when , about ( a , q ) = 1 (a,q)=1 (a,q)=1 You can do it as above , And for a q b aq^b aqb, Their connecting edges form a DAG, Ride by yourself log It will become 0, No ring , You can do it directly , Complexity O ( n log p ) O(n\log p) O(nlogp)
p p p The algorithm for odd primes can be further optimized , Conditions can be transformed into g c d ( x , φ ( p ) ) ∣ g c d ( y , φ ( p ) ) gcd(x,\varphi(p))~|~gcd(y,\varphi(p)) gcd(x,φ(p)) ∣ gcd(y,φ(p)), And for g x ≡ a g^x\equiv a gx≡a, Yes ord ( a ) = φ ( p ) g c d ( x , φ ( p ) ) \text{ord}(a)=\frac {\varphi(p)}{gcd(x,\varphi(p))} ord(a)=gcd(x,φ(p))φ(p), ord(a) \text{ord(a)} ord(a) yes a a a The order of , Even if have to a k ≡ 1 ( m o d p ) a^k\equiv 1\pmod p ak≡1(modp) The smallest k k k, Even if have to x k ≡ 0 ( m o d φ ( p ) ) xk\equiv 0\pmod {\varphi(p)} xk≡0(modφ(p)) The smallest k k k.
So we just need to find out ord ( a ) \text{ord}(a) ord(a) that will do , because ord ( a ) ∣ φ ( p ) \text{ord}(a)~|~\varphi(p) ord(a) ∣ φ(p), We can try to divide with the qualitative factor of the latter , Complexity O ( n log 2 p ) O(n\log^2p) O(nlog2p).
Code:
#include<bits/stdc++.h>
#define maxn 5005
using namespace std;
const int mod = 998244353;
int n,p,q,phi,d[35],pw[maxn],ans;
int v1[maxn],s1,v2[maxn],s2,cnt[maxn];
map<int,int>id1,id2;
void Divide(){
for(int i=2;i*i<=q;i++) if(q%i==0) {
p=i;break;}
if(!p) p=q;
int x=phi=q-q/p;
for(int i=2;i*i<=x;i++) if(x%i==0) {
d[++*d]=i; while(x%i==0) x/=i;}
if(x>1) d[++*d]=x;
}
int Pow(int a,int b,int mod){
int s=1;for(;b;b>>=1,a=1ll*a*a%mod) b&1&&(s=1ll*s*a%mod);return s;}
int ord(int x){
int w=phi;
for(int i=1;i<=*d;i++)
while(w%d[i]==0&&Pow(x,w/d[i],q)==1) w/=d[i];
return w;
}
int main()
{
scanf("%d%d",&n,&q),Divide();
for(int i=pw[0]=1;i<=n;i++) pw[i]=pw[i-1]*2%mod;
for(int i=1,x;i<=n;i++){
scanf("%d",&x);
if(x%p==0) v2[++s2]=x,id2[x]=s2;
else{
x=ord(x);
if(!id1[x]) id1[x]=++s1,v1[s1]=x;
cnt[id1[x]]++;
}
}
for(int i=1;i<=s1;i++){
int sum=n;
for(int j=1;j<=s1;j++) if(v1[j]%v1[i]==0) sum-=cnt[j];
ans=(ans+1ll*pw[sum]*(pw[cnt[i]]-1))%mod;
}
for(int i=1;i<=s2;i++) cnt[i]=n;
for(int i=1;i<=s2;i++)
for(int x=v2[i];x;x=1ll*x*v2[i]%q)
cnt[id2[x]]--;
for(int i=1;i<=s2;i++) ans=(ans+pw[cnt[i]])%mod;
printf("%d\n",ans);
}
边栏推荐
- Miscellany C language
- Calculate the day of the week for a specific month, year and day
- CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
- MS office level II wrong question record [7]
- Sdl-4 PCM playback
- Biological sequence intelligent analysis platform blog (1)
- [STL source code analysis] summary notes (5): a good helper for understanding iterators --list
- C language to write a calculator MVC (very interesting code architecture callback and constructor mode and the use of pointer functions)
- 【AtCoder2304】Cleaning
- QT interface nested movement based on qscrollarea
猜你喜欢

2022低压电工考题及在线模拟考试
![[analysis of STL source code] summary note (4): behind the scenes hero allocator](/img/b9/cf53fd8f933042ff65844d61eca55e.jpg)
[analysis of STL source code] summary note (4): behind the scenes hero allocator

2022 melting welding and thermal cutting exam exercises and answers

Leetcode-104. Maximum Depth of Binary Tree

big. Js-- use / instance

【编译原理】05-语法制导的语义计算——基于翻译模式的语义计算

【AtCoder2306】Rearranging(拓扑)

二、用户登录和注册

C language inherits memory management mechanism (unfinished)

Raspberry pie builds a full-featured NAS server (07): manage your library & read as you please
随机推荐
Multi thread review summary parsing volatile keyword
Several transaction modes of Seata
【CF #277.5 (Div. 2)】B. BerSU Ball
2022 melting welding and thermal cutting exam exercises and answers
Adventure of small X
P3172 [cqoi2015] data selection (Mobius inversion + Du Jiao sieve)
[STL source code analysis] summary notes (5): a good helper for understanding iterators --list
2022 low voltage electrician test questions and online simulation test
Biological sequence intelligent analysis platform blog (1)
[Oracle database] mammy tutorial day04 Sorting Query
MS office level II wrong question record [8]
Mistakes in Niuke JS exercise
No response from win10 explorer when dragging files
C language inherits memory management mechanism (unfinished)
【POJ3691】DNA repair (AC自动机+DP)
Mobile console Gobang (first draft of detailed design)
Nim product
Cartland number application
【AtCoder2376】Black and White Tree(博弈)
Leetcode-141. Linked List Cycle