BZOJ-1498: [NOI2006]神奇的口袋

作者: AmadeusChan | 来源:发表于2019-03-15 09:56 被阅读0次

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1498

我们神奇的发现把x[1],x[2]...x[n]映射到1,2,...n是等价的,所以直接算就好了嗯。

代码:

#include <cstdio>

#include <algorithm>

#include <cstring>

 

using namespace std ;

 

const int maxn = 20000 ;

 

bool flag[ maxn + 10 ] ;

int p[ maxn ] , pn = 0 ;

 

inline void Init(  ) {

        memset( flag , true , sizeof( flag ) ) ;

        for ( int i = 2 ; i <= maxn ; ++ i ) if ( flag[ i ] ) {

                p[ ++ pn ] = i ;

                for ( int j = i << 1 ; j <= maxn ; j += i ) flag[ j ] = false ;

        }

}

 

inline int gcd( int x , int y ) {

        if ( x < y ) swap( x , y ) ;

        for ( int z ; y ; z = y , y = x % y , x = z ) ;

        return x ;

}

 

struct NUM {

        int cnt[ maxn ] ;

        NUM(  ) {

                memset( cnt , 0 , sizeof( cnt ) ) ;

        }

        inline void add( int x ) {

                for ( int i = 0 ; i ++ < pn ; ) if ( x == 1 ) break ; else

                while ( ! ( x % p[ i ] ) ) {

                        ++ cnt[ i ] , x /= p[ i ] ;

                }

        }

} n1 , n2 ;

 

struct Bignum {

        int n , a[ maxn ] ;

        inline void Init(  ) {

                memset( a , 0 , sizeof( a ) ) ; a[ n = 1 ] = 1 ;

        }

        inline void mul( int x ) {

                for ( int i = 0 ; i ++ < n ; ) a[ i ] *= x ;

                for ( int i = 0 ; i ++ < n ; ) if ( a[ i ] >= 10 ) {

                        a[ i + 1 ] += a[ i ] / 10 ; a[ i ] %= 10 ;

                }

                while ( a[ n + 1 ] ) {

                        ++ n ;

                        if ( a[ n ] >= 10 ) {

                                a[ n + 1 ] += a[ n ] / 10 ; a[ n ] %= 10 ;

                        } else break ;

                }

        }

        inline void write(  ) {

                for ( int i = n ; i ; -- i ) printf( "%d" , a[ i ] ) ;

        }

} m ;

 

inline void print( NUM &x ) {

        m.Init(  ) ;

        for ( int i = 0 ; i ++ < pn ; ) for ( int j = 0 ; j ++ < x.cnt[ i ] ; ) {

                m.mul( p[ i ] ) ;

        }

        m.write(  ) ;

}

 

int n , t , d , a[ maxn ] , y[ maxn ] ;

 

int main(  ) {

        Init(  ) ;

        scanf( "%d%d%d" , &t , &n , &d ) ;

        int tot = 0 ;

        for ( int i = 0 ; i ++ < t ; ) {

                scanf( "%d" , a + i ) ; tot += a[ i ] ;

        }

        int x ; for ( int i = 0 ; i ++ < n ; ) scanf( "%d%d" , &x , y + i ) ;

        for ( int i = 0 , g , px , py ; i ++ < n ; ) {

                px = a[ y[ i ] ] , py = tot ; g = gcd( px , py ) ;

                px /= g , py /= g ; n1.add( px ) , n2.add( py ) ;

                a[ y[ i ] ] += d , tot += d ;

        }

        for ( int i = 0 ; i ++ < pn ; ) if ( n1.cnt[ i ] && n2.cnt[ i ] ) {

                if ( n1.cnt[ i ] > n2.cnt[ i ] ) {

                        n1.cnt[ i ] -= n2.cnt[ i ] ; n2.cnt[ i ] = 0 ;

                } else {

                        n2.cnt[ i ] -= n1.cnt[ i ] ; n1.cnt[ i ] = 0 ;

                }

        }

        print( n1 ) ; printf( "/" ) ; print( n2 ) ; printf( "\n" ) ;

        return 0 ;

}

相关文章

  • BZOJ-1498: [NOI2006]神奇的口袋

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1498 我...

  • 神奇口袋

    小时候 羡慕哆啦A梦 因为他的神奇口袋 能帮人实现愿望 长大了 明白 世上没有哆啦A梦 没有装满宝贝的神奇口袋 凡...

  • 神奇的口袋

    解析 一道典型的动态规划问题,首先说明下,面对一个物品的时候,你能做的选择只有两个:选or不选,那么选不选的标准是...

  • 神奇的口袋

    (兜兜编的第一个故事) 水果小镇来了一个陌生人,背着一个大口袋。他站在广场上,大声喊:“不要不要,要不要?神奇口袋...

  • 神奇的口袋

    今天上语文课的时候,胡老师跟我们讲了他昨天做梦时的事情,梦是这样的: 有一天胡老师在外面散步。突然...

  • 神奇的口袋

    今天上语文课的时候,胡老师告诉我们,他昨天做了一个非常奇怪的梦。 他梦见自己在散步,走着走着,他被一个东西绊倒了,...

  • 《神奇的口袋》

    今天的语文课上,老师讲了昨天他做的梦,接下来他问我们:“你们猜我昨天从口袋里摸出什么?请自己发挥想象。”接下...

  • 《神奇的口袋》

    今天我们胡老师给我们讲了她昨天梦见的梦,我们一个个都非常的认真听,因为胡老师讲的可好了。 ...

  • ?神奇的口袋

    上一次老师给我了评论,他说要放开一点想,那我就再放开一点。 我觉得可能是阿拉丁神灯,因为每个人都...

  • 🎒神奇的布口袋

    神奇的布口袋 也许是因为这段时间一直带孩子们学习神话单元的缘故吧,我的脑子里经常会冒出一些奇异的念头和想法来。有时...

网友评论

    本文标题:BZOJ-1498: [NOI2006]神奇的口袋

    本文链接:https://www.haomeiwen.com/subject/qeokmqtx.html