提交时间:2022-08-08 11:35:55
运行 ID: 54960
#include <bits/stdc++.h> using namespace std; #define ll long long inline void Read(ll &x) { char ch=getchar();x=0; while(!isdigit(ch)) ch=getchar(); while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); } int main() { // (2l+n-1) \times n /2-->p // freopen("number.in","r",stdin); // freopen("number.out","w",stdout); ll T,n,p; scanf("%lld",&T); while(T--) { scanf("%lld%lld",&n,&p); if(p==1) { puts("-1"); continue; } if(n==1) { printf("%lld\n",p); continue; } if(n%p==0) { if(n>p) puts("1"); else puts("-1"); } else { if(p%2==0 && n%2==0) puts("-1"); else if(n&1) printf("%lld\n",(p*2ll-n+1ll)/2ll); else printf("%lld\n",(p-n+1)/2ll); } } return 0; }