BZOJ1407: [Noi2002]Savage exgcd
Description

Input
Output
仅包含一个数M,即最少可能的山洞数。输入数据保证有解,且M不大于10^6。
Sample Input
1 3 4
2 7 3
3 2 1
Sample Output
6
//该样例对应于题目描述中的例子。
Solution
对我来说很清奇的一个思路:枚举答案。我自己推了一下式子可以推出来exgcd,但是就是想不到枚举那个m,还是要多读题..
式子其实不难推
求对于最小的m ,满足
$$c_1+(p_1*x)\space mod\space m = c_2 + (p_2*x)\space mod \space m $$
的同时,满足$x>min(l_1,l_2)$
上式可化为:
$$p_1*x\space mod\space m -p_2*x\space mod\space m=c2-c1 $$
$$x*(p_1-p_2)-ym=c2-c1$$
对于x,满足$x>min(l1,l2)$
所以就化为了exgcd的标准形式,记得把x弄成最小整数解就好。
#include <bits/stdc++.h> #define ll long long
#define inf 0x3f3f3f3f
#define il inline namespace io { #define in(a) a=read()
#define out(a) write(a)
#define outn(a) out(a),putchar('\n') #define I_int int
inline I_int read() {
I_int x = , f = ; char c = getchar() ;
while( c < '' || c > '' ) { if( c == '-' ) f = - ; c = getchar() ; }
while( c >= '' && c <= '' ) { x = x * + c - '' ; c = getchar() ; }
return x * f ;
}
char F[ ] ;
inline void write( I_int x ) {
if( x == ) { putchar( '' ) ; return ; }
I_int tmp = x > ? x : -x ;
if( x < ) putchar( '-' ) ;
int cnt = ;
while( tmp > ) {
F[ cnt ++ ] = tmp % + '' ;
tmp /= ;
}
while( cnt > ) putchar( F[ -- cnt ] ) ;
}
#undef I_int }
using namespace io ; #define N 100010 int n , mod , c[N] , p[N] , l[N] ; int x , y ;
int exgcd(int a , int b) {
if(b == ) {x = , y = ; return a;}
int ans = exgcd(b , a % b), tmp = x ;
x = y; y = tmp - (a / b) * y;
return ans ;
} int main() {
int mx = ; n = read() ;
for(int i = ; i <= n; i ++) {
c[i] = read() , p[i] = read() , l[i] = read() ;
mx = std::max(mx , c[i]) ;
} mod = mx - ;
while() {
mod ++ ;
int flag = ;
for(int i = ; i <= n ; i ++) {
for(int j = i + ; j <= n ; j ++) {
x = , y = ;
int k = ((p[i] - p[j]) % mod + mod) % mod , tmp = c[j] - c[i] ;
int gcd = exgcd(k , mod);
if(tmp % gcd) continue ;
int t = mod ;
x *= tmp / gcd ; t /= gcd ; t = std::abs(t) ;
x = ((x % t) + t) % t;
if(x <= std::min(l[i] , l[j])) {flag = ; break ;}
}
if(flag) break ;
}
if(!flag) { outn(mod) ; return ; }
}
}
BZOJ1407: [Noi2002]Savage exgcd的更多相关文章
- BZOJ1407 NOI2002 Savage 【Exgcd】
BZOJ1407 NOI2002 Savage Description Input 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci, Pi, L ...
- 【数学 exgcd】bzoj1407: [Noi2002]Savage
exgcd解不定方程时候$abs()$不能乱加 Description Input 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci, Pi, L ...
- BZOJ1407 [Noi2002]Savage 【扩展欧几里得】
题目链接 BZOJ1407 题解 枚举\(m\)用扩欧判即可 #include<algorithm> #include<iostream> #include<cstrin ...
- 【BZOJ 1407】[Noi2002]Savage ExGCD
我bitset+二分未遂后就来用ExGCD了,然而这道题的时间复杂度还真是玄学...... 我们枚举m然后对每一对用ExGCD判解,我们只要满足在最小的一方死亡之前无解就可以了,对于怎么用,就是ax+ ...
- bzoj1407 [Noi2002]Savage——扩展欧几里得
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1407 看到一定有解,而且小于10^6,所以可以枚举: 判断一个解是否可行,就两两判断野人 i ...
- [BZOJ1407][NOI2002]Savage(扩展欧几里德)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...
- BZOJ1407 [Noi2002]Savage
Description Input 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci, Pi, Li表示每个野人所住的初始洞穴编号,每年走过的洞穴 ...
- [Noi2002]Savage
[Noi2002]Savage 数学题. 题解回去写(有个坑点) flag++ #include <cstdio> int n,m,c[25],p[29],l[29]; int exgcd ...
- [Noi2002]Savage 题解
[Noi2002]Savage 时间限制: 5 Sec 内存限制: 64 MB 题目描述 输入 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci ...
随机推荐
- PHP主动断开与浏览器的连接
以前整理过一篇<关于PHP连接处理中set_time_limit().connection_status()和ignore_user_abort()深入解析>,是解说浏览器client断开 ...
- HDU1712:ACboy needs your help(分组背包模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1712 Problem Description ACboy has N courses this term, an ...
- PAT 1042 Shuffling Machine[难]
1042 Shuffling Machine (20)(20 分) Shuffling is a procedure used to randomize a deck of playing cards ...
- Google面试题[一]
谷歌是不少IT人都想去的企业,那么在进入公司前,少不了面试笔试的测试.那么这里我们就总结了如下谷歌笔试题,并提供了一些参考答案.希望对您有用. 谷歌笔试题:判断一个自然数是否是某个数的平方.当然不能使 ...
- windows7触屏编程
每当用户触摸触敏式 Windows 7 设备时,Windows 7 多点触控平台都会向您的应用程序发送手势消息 WM_GESTURE.这是现成的免费行为,如果您希望停止接收此类消息,则需要选择退出. ...
- [LeetCode] 243. Shortest Word Distance_Easy
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- 菜刀php过waf
关于PHP 一.waf为啥会拦截菜刀.菜刀在连接时,会向server端POST数据,抓包查看: 会看到他向server端post了 @eval(base64_decode($_POST[z0])); ...
- VMware Coding Challenge: Removing Duplicates Entries
static LinkedListNode removeDuplicates(LinkedListNode list) { LinkedListNode cur = list; HashSet< ...
- BCB ERROR:[Linker Error] 'XXX.LIB' contains invalid OMF record, type 0x21 (possibly COFF)
今天C++builder 导入 gts .lib (gts.dll)库文件 编译报错: [Linker Error] 'D:\...\V4.05.007.1000-20161028\GTS.LIB' ...
- 在liferay中如何使用Ajax的请求
1:首先在界面上写一个路径,这个路径就是要找后台中的哪一个操作比如: