Breaking up is wish: dp

Description

Zeit und Raum trennen dich und mich.
Time and space will separate you and me.

Mr. B playing a game, the game n lamps and n switches composed of the n given the 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 represent 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 i when switching, all the i number divisors (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 may be less than equal to k by operating the switches so that all lights exterminate

Then he will no longer be a random, direct selective operation method of operating a minimum number (k in this strategy is clearly less steps) the operation of these switches.

Mr. B would like to know this strategy in accordance with (i.e. the first random operation, less than or equal k and finally step, the minimum number of operations using the operating method) of the desired number of operations.

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

1 <= n <= 100000,0 <= k <= n. For 50% of the data, k = n.

 

This problem is also cushions for a long time, ah, rare title change fast today, come back and it did.

However, I decadent solution to a problem, and thought several times did not want to come out, uh. . . Solution to a problem and then how decadent than simply not doing some of the

 

The main difficulty of this problem is how to determine the status of the definition, in fact, the other better.

See the sub-portion of 50% (data water, and could get 80 ...)

That is what we consider optimal decision yes.

First of all, you operate a small number of switches, a large number of light bulbs that do not respond.

So for the largest number of bright lights, you want it to burn out, only two ways.

It is off by a switch, the other is off by more than its switch.

But because this is the brightest in the largest numbers, so if you switch by a larger number, then the brightest bulb in the sector number is even greater.

So sooner or later will rise to about n, then this time we can only turn it off yourself. . . Until then restore the original state.

So of course you will press directly out of its own.

After this the largest number of lighting smaller number, you can continue the same way to solve the problem.

So we got this sub-section.

But until here, and we did not dp anything.

But we can find some properties:

One of the switches, is not to be equivalent to the other set of switches instead.

In this case, we are given an initial state, we can obtain copies of the same portion as that it requires a set of switches.

In this case, we can assert that you need to move the switch, the switch that you can not move.

And it is a random operation, so if you can not move those switches move. . . Then you have to move again once it reinstated

This is the XOR operation, with the "operation not operate even number of times equal to" and "swap operation sequence result unchanged" in nature.

Here, and we began to construct meaning dp array.

We found that, in the end what is now the switch is not important, only two switch: You need to move, you can not move

So in fact, you only need to know that you need to move a few switches on it

DP provided $ [i] i $ denotes switches also need to operate, like a switch according to how many times a desired operation. Consider the transfer:

You have $ \ frac {i} {n} $ probabilities in pairs, then that is $ \ frac {i} {n} $

You have $ \ probability frac {ni} {n} $ by mistake, this time by the need to become a $ i + 1 $ a,

Then press down to $ 1 $ $ i + 1 $ step, and then come back is $ dp [i + 1] $, and you have to press a fall, is $ dp [i] $.

于是$dp[i]=\frac{i}{n} + \frac{n-i}{n} \times (dp[i+1]+dp[i]+1)$

The merger of similar items $ $ dp [i], then the coefficient of about give $ dp [i] = 1 + \ frac {ni} {i} \ times (dp [i + 1] +1) $

Then the answer is to put the original one by cnt into k, then use the k optimal decision sentence out.

$ans=k+\sum\limits_{i=k+1}^{cnt} dp[i]$

Of course, if cnt <= k, then the answer is cnt ah.

Finally, in accordance with the meaning of the questions multiplied by $ n! $ Can.

 1 #include<cstdio>
 2 #define mod 100003
 3 #define int long long
 4 int dp[mod],st[mod],cnt,n,k,ans;
 5 int qp(int b,int t,int a=1){for(;t;t>>=1,b=b*b%mod)if(t&1)a=a*b%mod;return a;}
 6 main(){
 7     scanf("%lld%lld",&n,&k);
 8     for(int i=1;i<=n;++i)scanf("%lld",&st[i]);
 9     for(int i=n;i;--i)if(st[i]){
10         cnt++;
11         for(int j=1;j*j<=i;++j)if(i%j==0){
12             st[j]^=1;if(j*j!=i)st[i/j]^=1;
13         }
14     }
15     if(cnt<=k){
16         for(int i=n;i;--i)cnt=cnt*i%mod;
17         printf("%lld\n",cnt);
18         return 0;
19     }
20     dp[n]=1;
21     for(int i=n-1;i;--i)dp[i]=((n-i)*qp(i,mod-2)%mod*(dp[i+1]+1)+1)%mod;
22     for(int i=k+1;i<=cnt;++i)ans=(ans+dp[i])%mod;ans+=k;
23     for(int i=n;i;--i)ans=ans*i%mod;
24     printf("%lld\n",ans);
25 }
View Code

 

Good question, very good idea.

In fact, so smoothly down seemingly not difficult, but why could not think of it?

The distance between my ideas and positive solutions. . .

Also you need more practice ah.

 

Guess you like

Origin www.cnblogs.com/hzoi-DeepinC/p/11669597.html