Description

Input

第1行为一个整数N(1<=N<=15),即野人的数目。
第2行到第N+1每行为三个整数Ci, Pi, Li表示每个野人所住的初始洞穴编号,每年走过的洞穴数及寿命值。
(1<=Ci,Pi<=100, 0<=Li<=10^6 )

Output

仅包含一个数M,即最少可能的山洞数。输入数据保证有解,且M不大于10^6。

Sample Input

3
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的更多相关文章

  1. BZOJ1407 NOI2002 Savage 【Exgcd】

    BZOJ1407 NOI2002 Savage Description Input 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci, Pi, L ...

  2. 【数学 exgcd】bzoj1407: [Noi2002]Savage

    exgcd解不定方程时候$abs()$不能乱加 Description Input 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci, Pi, L ...

  3. BZOJ1407 [Noi2002]Savage 【扩展欧几里得】

    题目链接 BZOJ1407 题解 枚举\(m\)用扩欧判即可 #include<algorithm> #include<iostream> #include<cstrin ...

  4. 【BZOJ 1407】[Noi2002]Savage ExGCD

    我bitset+二分未遂后就来用ExGCD了,然而这道题的时间复杂度还真是玄学...... 我们枚举m然后对每一对用ExGCD判解,我们只要满足在最小的一方死亡之前无解就可以了,对于怎么用,就是ax+ ...

  5. bzoj1407 [Noi2002]Savage——扩展欧几里得

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1407 看到一定有解,而且小于10^6,所以可以枚举: 判断一个解是否可行,就两两判断野人 i ...

  6. [BZOJ1407][NOI2002]Savage(扩展欧几里德)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...

  7. BZOJ1407 [Noi2002]Savage

    Description Input 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci, Pi, Li表示每个野人所住的初始洞穴编号,每年走过的洞穴 ...

  8. [Noi2002]Savage

    [Noi2002]Savage 数学题. 题解回去写(有个坑点) flag++ #include <cstdio> int n,m,c[25],p[29],l[29]; int exgcd ...

  9. [Noi2002]Savage 题解

    [Noi2002]Savage 时间限制: 5 Sec  内存限制: 64 MB 题目描述 输入 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci ...

随机推荐

  1. java Arrays.asList 问题

    1.问题 public static void asList() { System.out.println(Arrays.asList(new String[] { "a", &q ...

  2. 【Swift初见】SourceKitService Terminated

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/weasleyqi/article/details/36162085 心血来潮想试试最新的Xcode6 ...

  3. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  4. Postman接口自动化--Postman Script脚本功能使用详解

    Postman Script 功能,支持原生的JS,所以可以使用JS解决很多接口自动化的一些问题,例如接口依赖.接口参数专递和接口断言等: 这里主要是针对Pre-Request Script 和 Te ...

  5. MFC六大核心机制之五、六:消息映射和命令传递

    作为C++程序员,我们总是希望自己程序的所有代码都是自己写出来的,如果使用了其他的一些库,也总是千方百计想弄清楚其中的类和函数的原理,否则就会感觉不踏实.所以,我们对于在进行MFC视窗程序设计时经常要 ...

  6. Intermediate Python for Data Science learning 1 - Basic plots with matplotlib

    Basic plots with matplotlib from:https://campus.datacamp.com/courses/intermediate-python-for-data-sc ...

  7. REST服务安全-双向认证

    1. 创建服务器密钥,其密钥库为 d:/mykeys/server.ks,注意keypass和storepass保持一致,它们分别代表 密钥密码和密钥库密码,注意 CN=localhost 中,loc ...

  8. python 文件操作 练习:把一个目录下的所有文件名,打印一下,不要包含后缀名

    #coding=utf-8 import osos.chdir('d:\\test2')file_list=os.listdir('.')print "file_list:",fi ...

  9. tomcat和jetty区别

    参见:https://www.cnblogs.com/fengli9998/p/7247559.html 1. Jetty更轻量级.这是相对Tomcat而言的. 由于Tomcat除了遵循Java Se ...

  10. Linux基础命令---ckconfig

    chkconfig 启动或者关闭系统服务,设置服务的运行级别,该指令并不会立刻启动或者停止服务,而是在开机的时候发生效果. chkconfig提供了一个简单的命令行工具,用于维护/etc/rc[0-6 ...