# X问题

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2587    Accepted Submission(s): 817

Problem Description

Input

Output

Sample Input

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

Sample Output

1

0

3

Author

lwg

Source

HDU 2007-1 Programming Contest

http://acm.hdu.edu.cn/showproblem.php?pid=1573

``` 1 #include<iostream>
2 #define LL long long
3 #include<cstdio>
4 using namespace std;
5
6 LL x,y,q;
7 LL gcd(LL a,LL b)
8 {
9     if(b!=0)
10     return    gcd(b,a%b);
11     else
12        return a;
13 }
14 void exgcd(LL a,LL b)
15 {
16   if(b==0)
17         x=1 , y=0 , q=a;
18   else
19   {
20    exgcd(b,a%b);
21    LL temp=x;
22      x=y,y=temp-a/b*y;
23   }
24 }
25
26 int main()
27 {
28  int test,m,i;
29  int n,a[12],r[12];
30  LL lcm;
31  bool ifhave;
32  scanf("%d",&test);
33  while(test--)
34  {
35   scanf("%d%d",&n,&m);
36   lcm=1;
37   ifhave=true;
38   for(i=0;i<m;i++)
39   {
40     scanf("%d",a+i);
41     lcm=lcm/gcd(lcm,a[i])*a[i];  //求最小公倍数
42   }
43   for(i=0;i<m;i++)
44       scanf("%d",r+i);
45   for(i=1;i<m;i++)
46   {
47     exgcd(a[0],a[i]);
48     if((r[i]-r[0])%q)
49     {
50        ifhave=false;   //不需要在判断了
51        break;
52     }
53     LL t=a[i]/q;
54    x=((x*(r[i]-r[0])/q)%t+t)%t;
55    r[0]+=a[0]*x;
56    a[0]*=(a[i]/q);
57  }
58   if(!ifhave)
59   {
60       printf("0\n");
61   }
62   else
63   {
64       LL ans=0;
65       if(r[0]<=n)
66           ans=1+(n-r[0])/lcm;
67       if(ans&&r[0]==0)
68           ans--;
69       printf("%I64d\n",ans);
70   }
71  }
72  return 0;
73 }```

657 篇文章64 人订阅

0 条评论

## 相关文章

### HDUOJ 1099——Lottery

Lottery Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J...

3647

### HDU 2289 Cup【高精度，二分】

Cup Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/...

2505

### HDUOJ----Coin Change

Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

28911

### hdu 4009 Transfer water（最小型树图）

Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/657...

3689

### HDUOJ ------1398

http://acm.hdu.edu.cn/showproblem.php?pid=1398 Square Coins Time Limit: 2000/100...

2806

### HDU 3309 Roll The Cube(bfs)

Roll The Cube Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/327...

41513

### HDUOJ--------Text Reverse

Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65...

3467

### HDUOJ--1159Common Subsequence

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536...

2697

### HDUOJ------1711Number Sequence

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3...

3046