2019 hdu fourth Bu Ti (1, sign title

Because too dishes, the first few of the questions did not fill, fill slowly starting today

After the night to go to bed early, get up early in the morning, taking the time to read, only liver killing inning minaret every day, go to bed without playing mobile phone

Every Friday the whole week in summary print out the board, after all, the elderly memory

 

Bu Ti ascending order of difficulty

 

1001 AND Minimum Spanning Tree

The meaning of problems: the right side is defined in the two nodes and & configured lexicographically smallest minimum spanning tree

analysis:

1. stereotype algorithm

2. The default binary digits with leading 0, painting on paper just discovered structure adjacent the lowest point position 0 is set to 1, the remaining bits to 0 is the optimal solution, if the range is not directly set to 1

answer:

 

1001. AND Minimum Spanning Tree

 

This is a very simple problem. If N is 2^k-1, the cost of MST is 1, otherwise it’s 0. Lexicographically smallest solution is also trivial. 2*k’s parent is 1, 4*k+1’s parent is 2, 8*k+3’s parent is 4, and so on. If N is 2^k-1, only N’s parent is not determined after this process. In this case, N’s parent is 1.

Code:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<list>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef long double ld;
#define mem(x) memset(x, 0, sizeof(x))
#define me(x) memset(x, -1, sizeof(x))
#define fo(i,n) for(i=0; i<n; i++)
#define sc(x) scanf("%I64d", &x)
#define sca(n,m) scanf("%I64d%I64d", &n, &m)
#define pr(x) printf("%I64d\n", x)
#define pri(x) printf("%I64d ", x)
#define lowbit(x) x&-x
const ll MOD = 1e9 + 7;
const ll oo = 1e18;
const ll N = 4e5 + 5;
ll vis[N], a[N];
int main()
{
    ll i, j ,k;
    ll n, m, t, x;
    //cout<<(ll)pow(2,18)<<endl;

    cin>>t;
    while(t--)
    {
        cin>>n;
        mem(a);
        ll ans=0;
        for(i=2; i<=n; i++)
        {
            if(i%2)
            {
                k=0;x=i;
                while(1)
                {
                    if(x%2==0)
                    {
                        //k++;
                        break;
                    }
                    x>>=1;
                    k++;
                }
                k=1<<k;
                if(k<=n) a[i]=k;
                else a[i]=1, ans++;
            }
        }
        cout<<ans<<endl;
        for(i=2; i<n; i++)
        {
            if(a[i]<=1) cout<<1<<" ";
            else cout<<a[i]<<" ";
        }
        if(a[n]<=1) cout<<1<<endl;
        else cout<<a[n]<<endl;
    }

    return 0;
}

 

1007 Just an Old Puzzle

Meaning of the title: 15 Digital homing problem, to the point of initial state, ask whether the 120 steps can be homing

analysis:

1. If the topic is the shortest path, or you can use bfs A * algorithm, but 15 of the state too much nor write

2. This question is in fact so difficult

3. But just have a book of his teammates digital homing problem conclusion (!): The digital line arrangement, the target state reverse consistent with the parity of the number of initial state is solvable

4. As each step up to three states derived, 3 >> 120 ^ 16!, 120 steps to ensure that it is up to solvability

Like the title fans Solution:

 

 

1007. Just an Old Puzzle

 

The solution consists of three steps.

 

// about three steps

 

At first you have to match the numbers 1, 2, 3 and 4.

 

// 1. .3 .4 1.2 Match

 

To match these numbers, we only need the positions of 1, 2, 3, 4 and empty grid.

 

// match four, need to know where 1.2.3.4 and spaces

 

So the total number of possible states are P_16^5=524160.

 

// This is a total of 16 ^ 5 = 524,160 kinds of state

 

You can use BFS or any algorithms to find it.

 

// the minimum number of steps can be obtained within this range allows its homing or by other algorithms BFS

 

Next, you have to match the numbers 5, 6, 7 and 8.

 

// 2. Similarly, matching 5.6.7.8

 

And at last, you have to match the numbers 9~15.

 

// 3. 9-15 matched

 

The total possible statuses are 8! = 40320.

 

// a total of 8! = 40,320 kinds of state

 

You can check if you could find the solution by checking the parity of inversion number of input permutation.

 

//? ? Sudden jump) can be determined by the number of parity reverse Solving

 

You can easily prove that in first step, the maximum distance to target status are 46.

 

By using similar way, you can prove that you can match the grid in 120 moves.

 

// easy to prove, the first step 46 to step up, step 120 same reason of the reduction is ok

 

Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=16;
int ar[N],a[N];
int lowb(int t)
{
    return t & (-t) ;
}
void add(int i, int v)
{
    for ( ; i < N; ar[i] += v, i += lowb(i));
}
int sum(int i)
{
    int s = 0;
    for ( ; i > 0; s += ar[i], i -= lowb(i));
    return s;
}
int main()
{
    int n,m,T;
    scanf("%d",&T);
    while(T--)
    {
        n=4,m=4;
        int x,y,t,s=0,nu=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&t);
                if(t==0)
                    x=i,y=j;
                else
                    a[nu++]=t;
            }
        memset(ar,0,sizeof(ar));
        for(int i=nu-1;i>=0;i--)
        {
            s+=sum(a[i]-1);
            add(a[i],1);
        }
        if(m&1)
            if(s&1)puts("No");
            else   puts("Yes");
        else
            if(((n-x)^s)&1) puts("No");
            else   puts("Yes");
    }
    return 0;
}

 

 

 

1010 Minimal Power of Prime

The meaning of problems: a given set of data T (T <= 50000) n (n <= 1e18), seeking the lowest index n unique prime factors after decomposition;

analysis:

1. mathematics, greedy

2. Analysis of the prime factors of magnitude: 1e18 = 1e9 * 1e9, if there is no qualitative factors within 1e9, the rest of the index is certain is 1 (otherwise than 1e18 a), 1e9 too large timeout

3. Continue Analysis: 1e18 = 1e6 * 1e6 * 1e6 = 1e7 * 1e7 * (1e4) = 1e8 * 1e8 * (1e2), if no quality factor of the 1e6, the remaining constant index is 1 or 2, traversing 1e6 If not completely decomposed according to the left is not a square determination result (only 1 or 2), T timeout too large

4. Continue Analysis: = 1e18 1E4 * 1E4 * 1E4 * 1E4 * = 1E2 1E5 1E3 * 1E5 * 1E5 * = (P1: 1E4 * 1E4 ) * (P2: 1E4 1E4 * ) = (P1: 1E5 * 1E5) * ( p2: 1e5), if no quality factor of the 1e6, the remaining index necessarily 2,3,4 or 1, respectively, determines whether or not an integer, there are two prime factors of the square root only 1 case of the lowest index or 2, to determine whether to completely square, feeling two prime factors of the game there is no situation is too complicated to continue to analyze it, and found that because it is seeking the lowest index number of large prime factors result can only be 1 or 2 instead sentenced it is very simple orz, stupid people can not Fucong

5. Since the prescribing use a particularly large number POWL () is calculated to reduce the error, and the two parameters must be cast into long double (also constant, since the default Double), may request half prescribing a result, the precision error is in + 1 to -1, the result may be directly to p, p-1, p + 1 are determined is not exactly square root factor

 

answer:

 

1010. Minimal Power of Prime

 

Let's first factorize N using prime numbers not larger than N1/5. And let's denote M as the left part, all the prime factors of M are larger than N1/5. If M=1 then we are done, otherwise M can only be P2, P3, P4 or P2*Q2, here P and Q are prime numbers.

 

(1) If M1/4 is an integer, we can know that M=P4. Update answer using 4 and return.

 

(2) If M1/3 is an integer, we can know that M=P3. Update answer using 3 and return.

 

(3) If M1/2 is an integer, we can know that M=P2 or M=P2*Q2. No matter which situation, we can always update answer using 2 and return.

 

(4) If (1)(2)(3) are false, we can know that answer=1.

 

Since there are just O(N1/5/log(N)) prime numbers, so the expected running time is O(T*N1/5/log(N)).

 

Code:

#include<bits/stdc++.h>
 
#define fi first
#define se second
#define rep( i ,x ,y ) for( int i= x; i<= y ;i++ )
#define reb( i ,y ,x ) for( int i= y; i>= x ;i-- )
#define mem( a ,x ) memset( a ,x ,sizeof(a))
using namespace std;
 
typedef long long ll;
typedef long double ld; 
typedef pair<int ,int> pii;
typedef pair<ll ,ll> pll;
typedef pair<string ,int> psi;
 
const int inf = 0x3f3f3f3f;
const int N = 10005;
const int M = 200050;

int T ,ans ,tot=0;
int unprime[N];
ll n ,prime[N];

void euler( ){
    unprime[1] = 1;
    rep( i ,1 ,N ){
        if( !unprime[i] )prime[++tot] = i;
        rep( j ,1 ,tot ){
            if( i*prime[j] >N )break;
            unprime[ i*prime[j] ] = 1;
            if( i%prime[j] == 0 )break;
        }
    }
}

int main( ){
    scanf("%d" ,&T );
    euler( );
    //cout<<tot<<endl;
    while( T-- ){
        ans = inf;
        scanf( "%lld" ,&n );
        //cout<<n<<endl;
        rep( i ,1 ,tot ){
            if( n%prime[i]==0 ){
                int tmp = 0;
                while( n%prime[i] ==0 ){
                    n /= prime[i];
                    tmp++;
                }
                ans = min( ans ,tmp );
            }
            if( n==1 )break;
        }

        if( n >= N ){
            ll p1 ,p2 ,p3 ;
            ll p11 ,p22 ,p33;
            ll pp1 ,pp2 ,pp3;
            ld s = n; 
            p1 = powl( s ,(ld)1.0/4.0 );
            p11 = p1+1; pp1 = p1-1;
            p2 = powl( s ,(ld)1.0/3.0 );
            p22 = p2+1; pp2 = p2-1;
            p3 = sqrt( (ld)s );
            p33 = p3+1; pp3 = p3-1;
            //cout<<p1<<" "<<p2<<" "<<p3<<endl;
            //cout<<ans<<endl;
            if(N == p1 * p1 * p1 * n || p1 == * p11 * p11 p11 p11 || * n == pp1 pp1 * * * PP1 PP1) {years = min (years, 4 );}
             else  if ( n == p2 * p2 * p2 || == n * p22 p22 p22 * n || == * pP2 pP2 pP2 *) {years = min (year 3 );}
             else  if (n == * p3 p3 | | n == * p33 p33 || n == * PP3 PP3) {years = min (year 2 );}
             else years = min (year 1 ); 
        } 
        
        Printf ( " % d \ n " , year); 
    }     
    Return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/-ifrush/p/11282176.html