[Six provinces exam 2017] wish is to break up solution to a problem (expected dp)

Title Description

Mr. B playing a game, the game consists of  n  lamps and  n  switches composition, given that  n  initial state of the lamps, subscript from  1  to  n  a positive integer.

Each state has two lights on and off, we use  1 to indicate the lights are bright, with a  0 indicates that the lights are off, the object of the game is to make all the lights are put out.

However, when the operation of  the time switch i, all numbered  submultiple i (including  1 and  i) the state of the lamp will be changed, i.e., becomes off from light, or into the light off.

Mr. B found this game very difficult, so the thought of such a strategy, each operating a switch such as probability, until all the lights are put out.

The number of operations required a lot of this strategy, B-jun think of such an optimization. If the current situation, the operation can be less by  k switches all lights go out, then he will no longer be random, the minimum number of direct selective operation methods of operation (this strategy is clearly less than or equal  k  steps) the operation of these switches.

Mr. B would like to know this strategy in accordance with (i.e. the first random operation, less the last  k  steps, using a minimum number of operations of the method of operation) the desired number of operations.

This expectation may be large, but Mr. B found that this expectation is multiplied by  n  factorial must be an integer, so he only needs to know the integer of  100003  results after the modulo.

 

$Solution:$

Good God.

First consider how to make the optimal strategy is not difficult to draw a guess: from right to left to operate, shut it off.

Sensibility to understand it, certainly can not control a small number of large numbers, and each set of numbers were also affected by the operation of certain (regardless of its current state). Since the number of large and lit by sooner or later, but the impact is determined that it is better to press the state to determine the next treatment.

Further, this is clearly a maximum operating time for each lamp, so that the optimal number of steps must not greater than n.

For each state, the optimal strategy is uniquely determined, then we can use in the operand optimal strategy to represent each state.

If proper operation in the state $ I $, $ i $ becomes $ i-1 $. Conversely, if performed incorrectly, it becomes $ i + 1 $.

Part of the random, set $ dp [i] $ $ from a desired number of steps to $ i $ i-1 $ required.

The previous reasoning, it is easy to obtain: $ dp [i] = 1 \ times \ frac {i} {n} + \ frac {ni} {n} \ times (dp [i] + dp [i + 1] + 1) $

According to the step jump, press the wrong back $ i + 1 $, also you need $ dp [i + 1] + dp [i] + 1 $ to jump to $ i-1 $.

Simplification give $ dp [i] = \ frac {(ni) dp [i + 1] + n} {i} $, to seek a $ \ sum $, then the number of steps taken and the optimal strategy is to spell answer.

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int N=1e5+5;
typedef long long ll;
const ll mod=1e5+3;
int n,K,a[N];
vector<int> fact[N];
ll fac=1,ans,dp[N];
ll qpow(ll x,ll y)
{
    ll res=1;x=x%mod;
    while(y)
    {
        if(y&1)res=res*x%mod;
        x=x*x%mod;
        y>>=1;
    }
    return res;
}

void ini()
{
    for(int x=1;x<=n;x++)
    {
        fac*=1LL*x,fac%=mod;
        for(int i=1;i*i<=x;i++)
        {
            if(x%i)continue;
            if(i*i==x)fact[x].push_back(i);
            else fact[x].push_back(i),fact[x].push_back(x/i);
        }
    }
}

int main()
{
    scanf("%d%d",&n,&K);
    ini();
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    int step=0;
    for(int i=n;i;i--)
    {
        if(!a[i])continue;
        step++;
        for(int j=0;j<fact[i].size();j++)
            a[fact[i][j]]^=1;
    }
    if(step<=K)
    {
        printf("%lld\n",1LL*step*fac%mod);
        return 0;
    }
    for(int i=n;i>K;i--)
    {
        ll inv=qpow(i,mod-2);
        dp[i]=(1LL*(n-i)*dp[i+1]%mod+n%mod)%mod;
        (dp[i]*=inv)%=mod;
    }
    for(int i=K+1;i<=step;i++)
        (ans+=dp[i])%=mod;
    (ans+=K)%=mod;
    (ans*=fac)%=mod;
    cout<<ans<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Rorschach-XR/p/11619177.html