bzoj1407Noi2002savage数论

若第i个和第j个野人相聚,则有

P[ i ] * b + C[ i ] = P[ j ] * b + C[ j ] ( mod m )

m为洞的个数,移项得

( P[ i ] – P[ j ] ) * b + ( C[ j ] – C[ i ] ) * 1 == Gcd( 1 ,b  )

所以可以枚举洞的个数,然后快速检验是否可行

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <stack>
#include <deque>
#include <map>
#include <queue>
#define max( x , y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
#define min( x , y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
#define FOR( Tmp , St , Ed ) for ( int Tmp = St ; Tmp < Ed ; Tmp ++ )
#define FORD( Tmp , St , Ed ) for ( int Tmp = St ; Tmp > Ed ; Tmp -- )
const int max_int = ( ~0U >> 1 ) , oo = ( 1e9 ) ;
using namespace std ;

const int Maxn = ( 150 ) , Max_Ans = ( 1e6 ) ;
int N , C[ Maxn ] , P[ Maxn ] , L[ Maxn ] ;
int Pab[ Maxn ][ Maxn ] , Cab[ Maxn ][ Maxn ] ;

inline void Gcd( int a , int b , int &d , int &x , int &y )
{
	int c = a % b ;
	if ( c == 0 ) d = b , x = 0 , y = 1 ;
	else
	{
		Gcd( b , c , d , x , y ) ;
		int t = x ;
		x = y ;
		y = t - a / b * y ;
	}
	
	return ;
}

inline bool Check( int m )
{
	int a , b , d , x , y , mm ;
	
	FOR( i , 0 , N )
		FOR( j , i + 1 , N )
		{
			a = Pab[ i ][ j ] , b = Cab[ i ][ j ] ;
			Gcd( a , m , d , x , y ) ;
			if ( ( b % d ) != 0 ) continue ;
			b /= d , mm = m / d ;
			int ans = x * b ;
			while ( ans < 0 ) ans = ans + mm ;
			while ( ans >= mm ) ans = ans - mm ;
			if ( ans <= L[ i ] && ans <= L[ j ] ) return 0 ;
		}
	
	return 1 ;
}

int main()
{
	freopen( "savage.in" , "r" , stdin ) , freopen( "savage.out" , "w" , stdout ) ;
	
	scanf( "%d" , &N ) ;
	int cnt = 0 ;
	FOR( i , 0 , N )
	{
		scanf( "%d%d%d" , &C[ i ] , &P[ i ] , &L[ i ] ) ;
		C[ i ] -- ;
		cnt = max( cnt , C[ i ] ) ;
	}
	
	FOR( i , 0 , N )
		FOR( j , i + 1 , N )
		{
			Pab[ i ][ j ] = P[ i ] - P[ j ] ;
			Cab[ i ][ j ] = C[ j ] - C[ i ] ;
			if ( Pab[ i ][ j ] < 0 )
				Pab[ i ][ j ] *= - 1 , Cab[ i ][ j ] *= - 1 ;
		}
	
	FOR( Ans , cnt + 1 , Max_Ans )
		if ( Check( Ans ) )
		{
			cout << Ans << endl ;
			break ;
		}
	
	return 0 ;
}

数论真的太弱。。

转载于:https://www.cnblogs.com/yzxshuaige123/archive/2013/04/23/3036951.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值