链接:点击打开链接
题意:给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2},{N,1}.求出有多少个集合满足:第一个元素是A的倍数且第二个元素是B的倍数。提示:对于第二组测试数据,集合分别是:{1,10},{2,9},{3,8},{4,7},{5,6},{6,5},{7,4},{8,3},{9,2},{10,1}.满足条件的是第2个和第8个。
代码:
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
void exgcd(long long a,long long b,long long &d,long long &x,long long &y){
long long tmp;
if(b==0){
x=1,y=0;
d=a;
return;
}
exgcd(b,a%b,d,x,y);
tmp=x;
x=y;
y=tmp-(a/b)*y;
} //扩展欧几里得模板
int main(){
long long i,t,n,a,b,d,x,y,l,ans,tmp;
scanf("%I64d",&t); //求Ax+By=N+1中x满足条件的阶的个数
while(t--){
scanf("%I64d%I64d%I64d",&n,&a,&b);
n++;
exgcd(a,b,d,x,y);
l=a/d*b;
if(n%d){
puts("0");
continue;
}
ans=0;
x=x*(n/d)%b;
tmp=a*x;
while(tmp<1)
tmp+=l;
while(tmp>0)
tmp-=l;
tmp+=l; //找出第一个满足条件的值
if(tmp>=n){
puts("0");
continue;
}
// cout<<tmp<<endl;
printf("%d\n",(n-tmp-1)/l+1);
}
return 0;
}