Codeforces 402D D. Upgrading Array (dp+ number theory)

Source: Internet
Author: User

Topic Links:

Codeforces 402D

Main topic:

Given a sequence, you can do an operation to remove a prefix from their gcd, there is a function f (x), f (1) = 0, f (x) = f (x/p) +1,f (x) = f (x/p)-1 (p is bad prime) ask ∑ i≤n F (a[I]) The maximum value

Topic Analysis:
    • First, according to the recursive formula, we know that the result is that the F (x) of each number is the number of good primes in its mass factor minus the number of bad primes.
    • Then we know that if the number of good primes in a gcd is less than the number of bad primes, then we can get better results if we get rid of them, so if we start with the last number, the number of GCD will not affect the previous number, because if the number of bad primes in this part of the factorization is more than the good primes, The preceding number must also be removed.
    • So we are greedy of the reverse of doing just fine.
AC Code:
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <map>#define MAX 5007#define M 100007using namespace STD;intN,m,a[max],b[max];intPrime[m],g[max]; map<int,bool>mpvoidInit () {memset(Prime,0,sizeof(prime)); prime[1] = prime[0] =1; for(inti =2; I*i < M; i++) {if(Prime[i])Continue; for(intj = i*i; J < M; j+= i) prime[j] =1; }}intGCD (intAintb) {returnB?GCD (b,a%b): A;}intMain () {init (); while( ~scanf("%d%d", &n, &m)) {mp.clear (); for(inti =0; I < n; i++)scanf("%d", &a[i]); for(inti =0; I < m; i++)scanf("%d", &b[i]); g[0] = a[0]; for(inti =0; I < m; i++) Mp[b[i] =true; for(inti = n1; I >=0; i--) {intx =0; for(intj =0; J <= I; j + +) x = gcd (x, a[j]);intxx = x;intnum1,num2; NUM1 = Num2 =0; for(intj =2; J*j <= x; J + +) {if(Prime[j])Continue;if(X%J)Continue;BOOLflag = Mp[j]; while(X%j = =0) {x/= J;if(flag) num1++;Elsenum2++; }            }if(X >1)             {if(Mp[x]) num1++;Elsenum2++; }if(Num1 > Num2) for(intj =0; J <= I;        J + +) A[j]/= xx; }intAns =0; for(inti =0; I < n; i++) {intx = A[i]; for(intj =2; J*j <= x; J + +) {if(Prime[j])Continue;if(X%J)Continue;BOOLflag = Mp[j]; while(X%j = =0) {x/= J;if(flag) ans--;Elseans++; }            }if(X >1)            {if(Mp[x]) ans--;Elseans++; }        }printf("%d\n", ans); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 402D D. Upgrading Array (dp+ number theory)

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.