zoukankan      html  css  js  c++  java
  • LightOJ

    题目链接:https://vjudge.net/problem/LightOJ-1038

    1038 - Race to 1 Again
    Time Limit: 2 second(s) Memory Limit: 32 MB

    Rimi learned a new thing about integers, which is - any positive integer greater than 1 can be divided by its divisors. So, he is now playing with this property. He selects a number N. And he calls this D.

    In each turn he randomly chooses a divisor of D (1 to D). Then he divides D by the number to obtain new D. He repeats this procedure until D becomes 1. What is the expected number of moves required for N to become 1.

    Input

    Input starts with an integer T (≤ 10000), denoting the number of test cases.

    Each case begins with an integer N (1 ≤ N ≤ 105).

    Output

    For each case of input you have to print the case number and the expected value. Errors less than 10-6 will be ignored.

    Sample Input

    Output for Sample Input

    3

    1

    2

    50

    Case 1: 0

    Case 2: 2.00

    Case 3: 3.0333333333

    题意:

    给出一个数n,每次随机变为它的某一个因子(1~n),直到最后变为1。问n平均多少步操作到达1?

    题解:

    1.假设ci为n的因子,那么:dp[n] = (dp[1]+1 + dp[c2]+1 + dp[c3]+1 + ……  + dp[n]+1)/k,k为因子个数。

    2.由于左右两边都有dp[n],那么移项得:dp[n] = (dp[1] + dp[c2] + dp[c3] + ……  + k)/(k-1) 。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const int INF = 2e9;
    15 const LL LNF = 9e18;
    16 const int MOD = 1e9+7;
    17 const int MAXN = 1e5+100;
    18 
    19 double dp[MAXN];
    20 void init()
    21 {
    22     memset(dp, 0, sizeof(dp));
    23     for(int i = 2; i<MAXN; i++)
    24     {
    25         int cnt = 0; double sum = 0;
    26         for(int j = 1; j<=sqrt(i); j++) if(i%j==0) {
    27             sum += dp[j];
    28             cnt++;
    29             if(j!=i/j) sum += dp[i/j], cnt++;
    30         }
    31         dp[i] = 1.0*(sum+cnt)/(cnt-1);
    32     }
    33 }
    34 
    35 
    36 int main()
    37 {
    38     init();
    39     int T, n, kase = 0;
    40     scanf("%d", &T);
    41     while(T--)
    42     {
    43         scanf("%d",&n);
    44         printf("Case %d: %.10lf
    ", ++kase, dp[n]);
    45     }
    46 }
    View Code
  • 相关阅读:
    mysql问题小结
    mysql批量执行sql文件
    VMware使用中常见问题
    mybaits中xml文件大于号和小于号的处理方法
    自调用匿名函数的三种写法
    Linux相关文章
    Linux常用命令
    不触发事件,vue子组件传值给父组件
    elementUi使用单选框,并且单击行的时候选中该条数据
    可以和正则表达式一起使用的4个字符串方法
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8442459.html
Copyright © 2011-2022 走看看