4000 hdu1573 X问题 一元模线性方程组
2016-08-18 11:43
211 查看
/* 题目描述:给出含m(1<= m <= 10)个形如x=bi (mod ai)的模线性方程组,问该方程组小于等于(1 <= n <= 1e9) 的解有多少个? 方法:正常解一元模线性方程组,自上向下合并,直至仅剩一个方程x = r (mod lcm(a1 ,a2 ,...,am)),若r > n 无解; 否则令r+lcm*x <= n,解出x,若r不等于0则x++,x即为答案 */ #pragma warning(disable:4786) #pragma comment(linker, "/STACK:102400000,102400000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<map> #include<set> #include<vector> #include<cmath> #include<string> #include<sstream> #define LL long long #define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i) #define mem(a,x) memset(a,x,sizeof(a)) #define lson l,m,x<<1 #define rson m+1,r,x<<1|1 using namespace std; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; const double PI = acos(-1.0); const double eps=1e-8; LL a[15], b[15]; LL n , m; LL ex_gcd(LL a , LL b , LL &x ,LL &y) { if(b==0){ x = 1 ; y = 0; return a; } LL d = ex_gcd(b , a%b , y , x); y = y - a / b * x; return d; } LL solve() { LL a1 , r1 , a2 , r2 , x0 , y0; bool ok = 1; a1 = a[1]; r1 = b[1]; for(int i = 2 ; i <= m ; i++){ a2 = a[i] ; r2 = b[i]; LL d = ex_gcd(a1 , a2 , x0 , y0); LL c = r2 - r1; if(c % d){ ok = 0; } LL m= a2 / d; x0 = (x0 * c / d % m + m)%m; r1 = a1 * x0 + r1; a1 = a1 * (a2 / d); } if(!ok) return -1; else{ if(n < r1) return -1; LL ret = (n - r1 ) / a1 ; if(r1 !=0) ++ret ; return ret; } } int main() { int T ; scanf("%d",&T); while(T--){ scanf("%lld%lld",&n,&m); FOR(i , 1 , m) scanf("%lld",&a[i]); FOR(i , 1 , m) scanf("%lld",&b[i]); LL ans = solve(); if(ans != -1) printf("%lld\n",ans); else printf("0\n"); } return 0; }
相关文章推荐
- react native 动态添加/渲染组件
- 实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit())
- mac常用Terminal命令與快捷鍵參考
- #174 Remove Nth Node From End of List
- 正则表达式-切割
- csu 最优对称路径(bfs+记忆化搜索)
- swift 如何在IOS应用图标上添加消息数
- android 获取进程名字
- CF702E - Analysis of Pathes in Functional Graph
- 一个码农搬砖3年的总结
- #关于RACCommand的思考
- 如何获取android源码
- Adding Animations之Crossfading Two Views
- 关于PHP文件上传的一些问题
- js中addEventListener与attachEvent兼容
- A query was run and no Result Maps were found
- 大数据-计算引擎
- [LeetCode] Shuffle an Array 数组洗牌
- android.annotation.SuppressLint
- 1.Date对象