SRM 400 div1

Source: Internet
Author: User

This set of questions is cool.


250

Simple question. Can a number be expressed as the power of a prime number?

I used a method with high precision loss.

In fact, as long as the square is determined, it is OK to enumerate the prime numbers directly.

vector<int>ans;bool check(int x) {    int m = (int)sqrt(x * 1.0) + 1;    if(x == 2) return true;    for(int i = 2; i <= m; i++) {        if(x % i == 0) return false;    }    return true;}void gao(long long x) {    int x1 = -1, x2 = -1;    for(int i = 2; i < 60; i++) {        int f = (int)(pow((double)x, 1.0 / i) + eps);        long long tmp = 1;        for(int j = 0; j < i; j++)            tmp = tmp * (long long)f;        if(tmp == x) {            if(check(f)) {                x1 = f, x2 = i;            }        }    }    if(x1 != -1) {        ans.push_back(x1);        ans.push_back(x2);    }}



500

Interval DP

It means to give a string a and a string B.

Both contain 0 and 1, and then use some column reverse operations to convert A to B.

Reverse (I, j) indicates that the range I and j is reversed.

Then there is a limit on this series of operations

After an operation is completed, the next operation must be performed in the subinterval of the operation interval.

Then this is certainly convenient for the interval DP

I think someone has written a very violent DFS. I didn't dare to try it. I feel that complexity is not enough.

DP [k] [I] [J] [0] indicates that a substring whose start length is K at the position of string I does not flip into a substring whose start length is K at the position of string J. number of steps

DP [k] [I] [J] [1] indicates that the substring whose start length is k in string a is required to be flipped to the substring whose start length is k in position J in string B. steps

 int n = a.size();        memset(dp, 0x3f, sizeof(dp));        for(int j = 0; j <= n; j++)            for(int k = 0; k <= n; k++)                dp[0][j][k][0] = dp[0][j][k][1] = 0;        for(int i = 1; i <= n; i++)            for(int j = 0; j + i <= n; j++)                for(int k = 0; k + i <= n; k++) {                    if(a[j] == b[k]) {                        dp[i][j][k][0] = min(dp[i][j][k][0], dp[i - 1][j + 1][k + 1][0]);                    }                    if(a[j + i - 1] == b[k + i - 1]) {                        dp[i][j][k][0] = min(dp[i][j][k][0], dp[i - 1][j][k][0]);                    }                    if(a[j] == b[k + i - 1]) {                        dp[i][j][k][1] = min(dp[i][j][k][1], dp[i - 1][j + 1][k][1]);                    }                    if(a[j + i - 1] == b[k]) {                        dp[i][j][k][1] = min(dp[i][j][k][1], dp[i - 1][j][k + 1][1]);                    }                    dp[i][j][k][0] = min(dp[i][j][k][0], dp[i][j][k][1] + 1);                    dp[i][j][k][1] = min(dp[i][j][k][1], dp[i][j][k][0] + 1);            }        return dp[n][0][0][0] >= 1000 ? -1: dp[n][0][0][0];


1000

The formula is simple.

N * (1/n + 1/(n-1) + 1/(n-2) +... + 1/(n-k + 1 ))

The key issue is coming.

N and K are huge.

Then we found that this is a harmonic series sum.

When the number is large, only the approximate formula is used.

Try it.

(1/n + 1/(n-1) + 1/(n-2) +... + 1/(n-k + 1) is approximately equal to log (n + 1) + R

R is Euler's constant.


This formula is used when K is used up. Otherwise, the request is.

But wa is out of the box.


Finally, we found that the formula originally obtained by others is log (n + 1)/(n-k + 1 ))

Then there is a function called log1p. What is it? log1p (x) returns log (x + 1)

But the problem arises. when X is very small, the log1p has a high precision. When using log, x + 1 will lose the precision.

Then let's get together. It's not enough to use log1p. the denominator is reduced by 0.5, which is used to adjust the accuracy.

It hurts me.

I found a lot of log1p files in the room, and I gave them all to Cha.

 double expectedBuy(string n, string k)    {        long long x = gao(n);        long long y = gao(k);        double ans = 0;        long long s = x - y + 1;        long long mx = 10000000;        while(s <= mx) {            ans += 1.0 / s;            if(s == x) return x * ans;            s++;        }        ans += log1p((double)(x - s + 1) / (s - 0.5));        return ans * x;    }



SRM 400 div1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.