【CF1262F】Wrong Answer on test 233(数学)

题意:给定n道题目,每道题目有k个选项,已知所有正确选项,选对1题得1分

问循环后移一格后总得分s2大于原先总得分s1的方案数

n<=2e5,1<=k<=1e9

思路:特判k=1

easy版本的话写了dp

dp[i][j]表示前i道题循环后s2比s1高j的方案数,j=【-n,n】,n方dp即可

hard版本注意到得分的方案是对称的,即s1>s2和s1<s2的方案数相等

所以问题转化为统计s1=s2的方案数

设s1正确s2不正确的位置为-1,反之为1,其余为0

考虑不同的位置有s个

枚举+1的位置,假设有i个,方案数为C(s,i)

-1的位置同样有i个,方案数为C(s-i,i)

其他s-2*i个位置都是0,每个位置上有(k-2)种选择,方案数为(k-2)^(s-2*i)

剩余相同的位置每个位置上有k种选择,方案数为k^(n-s)

合法的总方案数有k^n种,总方案数-s1=s2的方案数之后除2即为所求

easy版本

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 typedef unsigned int uint;
 5 typedef unsigned long long ull;
 6 typedef long double ld;
 7 typedef pair<int,int> PII;
 8 typedef pair<ll,ll> Pll;
 9 typedef vector<int> VI;
10 typedef vector<PII> VII;
11 typedef pair<ll,ll>P;
12 #define N  200000+10
13 #define M  200000+10
14 #define INF 1e9
15 #define fi first
16 #define se second
17 #define MP make_pair
18 #define pb push_back
19 #define pi acos(-1)
20 #define mem(a,b) memset(a,b,sizeof(a))
21 #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
22 #define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
23 #define lowbit(x) x&(-x)
24 #define Rand (rand()*(1<<16)+rand())
25 #define id(x) ((x)<=B?(x):m-n/(x)+1)
26 #define ls p<<1
27 #define rs p<<1|1
28 #define fors(i) for(auto i:e[x]) if(i!=p)
29  
30 const int MOD=998244353,inv2=(MOD+1)/2;
31       //int p=1e4+7;
32       //double eps=1e-6;
33       int dx[4]={-1,1,0,0};
34       int dy[4]={0,0,-1,1};
35  
36 ll dp[2010][4100];
37 int a[N],b[N];
38  
39 int read()
40 {
41    int v=0,f=1;
42    char c=getchar();
43    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
44    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
45    return v*f;
46 }
47  
48 ll readll()
49 {
50    ll v=0,f=1;
51    char c=getchar();
52    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
53    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
54    return v*f;
55 }
56  
57 int main()
58 {
59     //freopen("1.in","r",stdin);
60     //freopen("1.out","w",stdout);
61     int n=read(),k=read();
62     rep(i,1,n) a[i]=read();
63     rep(i,1,n) b[i%n+1]=a[i];
64     if(k==1)
65     {
66         printf("0\n");
67         return 0;
68     }
69     int eps=n;
70     dp[0][0+eps]=1;
71  
72     rep(i,1,n)
73     {
74         if(a[i]==b[i])
75         {
76             rep(j,-n,n) dp[i][j+eps]=dp[i-1][j+eps]*k%MOD;
77             continue;
78         }
79         rep(j,-n,n) dp[i][j+eps]=(dp[i-1][j-1+eps]+dp[i-1][j+1+eps]+1ll*(k-2)*dp[i-1][j+eps]%MOD)%MOD;
80     }
81     ll ans=0;
82     rep(i,1,n) ans=(ans+dp[n][i+eps])%MOD;
83     printf("%I64d\n",ans);
84     return 0;
85 }

hard版本

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 typedef unsigned int uint;
  5 typedef unsigned long long ull;
  6 typedef long double ld;
  7 typedef pair<int,int> PII;
  8 typedef pair<ll,ll> Pll;
  9 typedef vector<int> VI;
 10 typedef vector<PII> VII;
 11 typedef pair<ll,ll>P;
 12 #define N  200000+10
 13 #define M  200000+10
 14 #define INF 1e9
 15 #define fi first
 16 #define se second
 17 #define MP make_pair
 18 #define pb push_back
 19 #define pi acos(-1)
 20 #define mem(a,b) memset(a,b,sizeof(a))
 21 #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
 22 #define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
 23 #define lowbit(x) x&(-x)
 24 #define Rand (rand()*(1<<16)+rand())
 25 #define id(x) ((x)<=B?(x):m-n/(x)+1)
 26 #define ls p<<1
 27 #define rs p<<1|1
 28 #define fors(i) for(auto i:e[x]) if(i!=p)
 29 
 30 const int MOD=998244353,inv2=(MOD+1)/2;
 31       //int p=1e4+7;
 32       //double eps=1e-6;
 33       int dx[4]={-1,1,0,0};
 34       int dy[4]={0,0,-1,1};
 35 
 36 ll fac[N],inv[N];
 37 int a[N],b[N];
 38 
 39 int read()
 40 {
 41    int v=0,f=1;
 42    char c=getchar();
 43    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
 44    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
 45    return v*f;
 46 }
 47 
 48 ll readll()
 49 {
 50    ll v=0,f=1;
 51    char c=getchar();
 52    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
 53    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
 54    return v*f;
 55 }
 56 
 57 ll C(int x,int y)
 58 {
 59     if(y>x) return 0;
 60     return fac[x]*inv[y]%MOD*inv[x-y]%MOD;
 61 }
 62 
 63 ll pw(ll x,ll y)
 64 {
 65     x%=MOD;
 66     ll res=1;
 67     while(y)
 68     {
 69         if(y&1) res=res*x%MOD;
 70         x=x*x%MOD;
 71         y>>=1;
 72     }
 73     return res;
 74 }
 75 
 76 int main()
 77 {
 78     //freopen("1.in","r",stdin);
 79     //freopen("1.out","w",stdout);
 80     int n=read(),k=read();
 81     rep(i,1,n) a[i]=read();
 82     rep(i,1,n) b[i%n+1]=a[i];
 83     if(k==1)
 84     {
 85         printf("0\n");
 86         return 0;
 87     }
 88     int s=0;
 89     rep(i,1,n)
 90      if(a[i]!=b[i]) s++;
 91     fac[0]=1;
 92     rep(i,1,2e5) fac[i]=fac[i-1]*i%MOD;
 93     inv[0]=inv[1]=1;
 94     rep(i,2,2e5) inv[i]=inv[MOD%i]*(MOD-MOD/i)%MOD;
 95     rep(i,1,2e5) inv[i]=inv[i-1]*inv[i]%MOD;
 96     ll ans=0,sum=0;
 97     rep(i,0,s/2)
 98     {
 99         ll tmp=C(s,i)*C(s-i,i)%MOD*pw(k-2,s-i*2)%MOD*pw(k,n-s)%MOD;
100         sum=(sum+tmp)%MOD;
101     }
102     ans=(pw(k,n)-sum+MOD)*inv2%MOD;
103     printf("%I64d\n",ans);
104     return 0;
105 }

 

posted on 2019-11-25 17:01  myx12345  阅读(472)  评论(0编辑  收藏  举报

导航