[UVA 11762] Race to 1
2286 단어 DP
확률 DP, 수학 문제
Dilu have learned a new thing about integers, which is - any positive integer greater than 1 can bedivided by at least one prime number less than or equal to that number. So, he is now playing withthis property. He selects a number N. And he calls this D.In each turn he randomly chooses a prime number less than or equal to D. If D is divisible by theprime number then he divides D by the prime number to obtain new D. Otherwise he keeps the oldD. He repeats this procedure until D becomes 1. What is the expected number of moves required forN to become 1.[We say that an integer is said to be prime if its divisible by exactly two different integers. So, 1 is nota prime, by definition. List of first few primes are 2, 3, 5, 7, 11, ...]
Input
Input will start with an integer T (T ≤ 1000), which indicates the number of test cases. Each of thenext T lines will contain one integer N (1 ≤ N ≤ 1000000).
Output
For each test case output a single line giving the case number followed by the expected number of turnrequired. Errors up to 1e-6 will be accepted.
Sample Input
3
1
3
13
Sample Output
Case 1: 0.0000000000
Case 2: 2.0000000000
Case 3: 6.0000000000
DP의 핵심은 dp[i]가 자신을 되돌릴 수 있다는 점입니다.이 때 수학적 변환을 해서 dp[i]를 등식의 왼쪽에 두어야 한다.
dp[i] = 1 + dp[i]*( 1- divn/ptot ) + sigma( dp[ i/div ] )/ptot ;
dp[i] = ( ptot + sigma( dp[ i/div ] ) )/divn;
ptot는 i보다 작은 질수 개수이고,divn은 i를 정제할 수 있는 질수 개수이며,div는 i를 정제할 수 있는 질수이다.
또한maxn은 비교적 커서 내가 처음 사용한 예처리는 본기마저 터졌다.기억화 검색으로 바꾸면 필요한 값만 계산하면 A가 된다.일반적인 DP는 모두 기억화 검색 형태로 할 수 있을 것 같아 효율성도 높이고 이해하기도 편해 배웠다.
#include
#include
#include
using namespace std;
const int maxn = 1000010, prinum = 99999;
bool nprime[maxn];
int plist[1500000];
int lp;
double dfs(int);
double dp[maxn];
bool dvis[maxn];
int main()
{
for(int i=2; i<=1001; i++)
{
if(!nprime[i])
{
int now = i*2;
while(now < maxn)
{
nprime[now] = 1;
now += i;
}
}
}
for(int i=2; i
2015.11.2
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[BOJ]11048(python)python 풀이 DP를 이용해 풀이 보통 이런 문제는 dfs나 bfs로 풀이하는 것이여서 고민을 했는데 이 문구 덕분에 DP 를 이용해 풀이할 수 있었다 뒤로 돌아가는 등의 경우를 고려하지 않아도 되기 때문이다 코...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.