Hdu_1573_X problem (segmentation: China Surplus, hdu_1573_x Segmentation

Source: Internet
Author: User

Hdu_1573_X problem (segmentation: China Surplus, hdu_1573_x Segmentation
Returns the number of X integers less than or equal to N. 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 ).

The first line of Input data is a positive integer T, indicating that there are T groups of test data. The first behavior of each group of test data is two positive integers N, M (0 <N <= 1000,000,000, 0 <M <= 10), indicating that X is less than or equal to N, the arrays a and B have M elements. In the next two rows, each row has M positive integers, which are elements in a and B respectively. Output corresponds to each input group, and a positive integer is Output in an independent row, indicating the number of X that meets the conditions.

Sample Input

310 31 2 30 1 2100 73 4 5 6 7 8 91 2 3 4 5 6 710000 101 2 3 4 5 6 7 8 9 100 1 2 3 4 5 6 7 8 9

Sample Output
103

In my opinion, it is a deformation Question of China's residual theorem, or a deformation question of lcm.
Each number that matches the answer is between 0 and N, and the distance is lcm. If lcm + x is set to the solution that meets all a [I], lcm + x also meets the condition.
Certificate:
(Lcm + x) % a [I] = B [I]
Lcm % a [I] = 0
(Lcm + x) % a [I] = lcm % a [I] + (lcm + x) % a [I] = B [I]
Set t = N % lcm
In this question, we can determine whether there is a solution between 0-t and t-lcm + t.
#include <iostream>#include<cstdio>using namespace std;#define ll long longint a[15];int b[15];int gcd(int a,int b){    return b==0?a:gcd(b,a%b);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n,m;        int lcm=1;        cin>>n>>m;        for(int i=0;i<m;i++)        {            cin>>a[i];            lcm=a[i]/gcd(lcm,a[i])*lcm;        }        for(int i=0;i<m;i++)        {            cin>>b[i];        }        int r=n%lcm;        int cnt1=0;        for(int i=1;i<=r&&!cnt1;i++)        {            for(int j=0;j<m;j++)            {                if(i%a[j]!=b[j])                    break;                if(j==m-1)                {                    cnt1++;                }            }        }        int cnt2=0;        for(int i=r+1;i<=r+lcm&&!cnt2;i++)        {            for(int j=0;j<m;j++)            {                if(i%a[j]!=b[j])                    break;                if(j==m-1)                    cnt2+=n/lcm;            }        }        cout<<cnt1+cnt2<<endl;    }}

  



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.