#include using namespace std; template void read(T &t) { t=0; char ch=getchar(); int f=1; while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); } do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f; } int T,a,b,dp[1010][1010]; int gcd(int x,int y) { if (!x||!y) return x+y; return gcd(y,x%y); } int main() { //freopen("1.txt","r",stdin); read(T); for (int i=1;i<=1000;i++) for (int j=1;j<=1000;j++) { dp[i][j]=max(dp[i][j-1],dp[i-1][j])+(gcd(i,j)==1); } while (T--) { read(a); read(b); printf("%d\n",dp[a][b]); } return 0; } /* 0. Enough array size? Enough array size? Enough array size? Interger overflow? 1. Think TWICE, Code ONCE! Are there any counterexamples to your algo? 2. Be careful about the BOUNDARIES! N=1? P=1? Something about 0? 3. Do not make STUPID MISTAKES! Time complexity? Memory usage? Precision error? */