HDU 1573: X问题

3
10 3
1 2 3
0 1 2
100 7
3 4 5 6 7 8 9
1 2 3 4 5 6 7
10000 10
1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9

1

0

3

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define range(i,a,b) for(int i=a;i<=b;++i)
#define rerange(i,a,b) for(int i=a;i>=b;--i)
#define LL long long
#define CLS(arr) memset(arr,0,sizeof(arr))
using namespace std;
int T;
int a[11],b[11];
void init(){
    cin>>T;
}
LL gcd(LL a,LL b){
    return !b?a:gcd(b,a%b);
}
void solve(){
    while(T--){
        int n,m;
        cin>>n>>m;
        LL lcm=1,ans=0;
        range(i,1,m){
            cin>>a[i];
            lcm=lcm*a[i]/gcd(lcm,a[i]);
        }
        range(i,1,m)cin>>b[i];
        range(i,n%lcm+1,n%lcm+lcm){
            bool flag=true;
            range(j,1,m)
            if(i%a[j]!=b[j]){
                flag= false;
                break;
            }
            ans+=flag?n/lcm:0;
        }
        range(i,1,n%lcm){
            bool flag=true;
            range(j,1,m)
                if(i%a[j]!=b[j]){
                    flag= false;
                    break;
                }
            ans+=flag?1:0;
        }
        cout<<ans<<endl;
    }
}
int main(int argc, char *argv[]){
    init();
    solve();
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/Rhythm-/p/9322624.html