[UVA 11762] Race to 1

2286 단어 DP
P143 트레이닝 가이드
확률 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

좋은 웹페이지 즐겨찾기