HDU
来源:互联网 发布:免费图片恢复软件 编辑:程序博客网 时间:2024/06/18 14:57
题目:
求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mod a[i] = b[i], … (0 < a[i] <= 10)。
思路:套模板就行了
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<ctime>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#include<numeric>using namespace std;#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f3f3f3f3f#define mm(a,b) memset(a,b,sizeof(a))#define PP puts("*********************");template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}// 0x3f3f3f3f3f3f3f3fLL r[15],a[15];void exgcd(LL a,LL b,LL &d,LL &x,LL &y){ if(b==0){ d=a;x=1;y=0; } else{ exgcd(b,a%b,d,y,x); y-=(a/b)*x; }}LL solve(int n){//求x≡ri(mod ai) LL nr=r[0],na=a[0],d,x,y; bool flag=true; for(int i=1;i<n;i++){ exgcd(na,a[i],d,x,y); LL c=r[i]-nr; if(c%d!=0) flag=false; LL t=a[i]/d; x=(x*(c/d)%t+t)%t; nr=na*x+nr; na=na*(a[i]/d); } if(!flag) return -1; return nr;}int main(){ LL N; int T,M; scanf("%d",&T); while(T--){ scanf("%lld%d",&N,&M); LL LCM=1; for(int i=0;i<M;i++){ scanf("%lld",&a[i]); LCM=lcm(LCM,a[i]); if(LCM>N) LCM=N+1; } for(int i=0;i<M;i++) scanf("%lld",&r[i]); LL x=solve(M); if(x==-1) printf("0\n"); else{ if(x==0) x+=LCM; int cnt=0; while(x<=N){ cnt++; x+=LCM; } printf("%d\n",cnt); } } return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- Vijos1375 大整数
- Android okHttp网络请求之Retrofit+Okhttp+RxJava组合
- html输入框只允许输入数字及小数点
- C++拷贝构造函数详解
- poj 3368
- HDU
- 带有卫星数据的排序(使用sort)
- 翻译&学习|ACL2017|Modeling Situations in Neural Chat Bots
- [js高手之路]性能优化技巧
- scala的认识
- Linux中一个删除用户的方法
- IE ActiveX设置
- [初学笔记] matlab的基本数据类型
- hdu-5768 Lucky7 容斥