#include #include #include #include #include using namespace std; const int ra=10; int ten[4]= {1,ra,ra*ra,ra*ra*ra}; int radix=ra*ra*ra*ra; const int NV=1000; struct integer { int d[NV]; integer() { *this=integer(0); } integer(int x)//int转大数 { for (int i=0; i=0; i--) { j=(len-i-1)/4+1; k=(len-i-1)%4; d[j]+=ten[k]*(s[i]-'0'); } while(d[0]>1&&d[d[0]]==0) d[0]--; } string tostring() { string s; int i,j,temp; for (i=3; i>=1; i--) if (d[d[0]]>=ten[i]) break; temp=d[d[0]]; for (j=i; j>=0; j--) { s+=(char) (temp/ten[j]+'0'); temp%=ten[j]; } for (i=d[0]-1; i>0; i--) { temp=d[i]; for (j=3; j>=0; j--) { s+=(char) (temp/ten[j]+'0'); temp%=ten[j]; } } return s; } void output()//输出 { int k=d[0]; printf("%d",d[k--]); while(k) printf("%04d",d[k--]); putchar('\n'); } } d,mid1[15]; bool operator <(const integer &a,const integer &b)//重载了比较 也就是说可以用lower bound { if (a.d[0]!=b.d[0]) return a.d[0]0; i--) if (a.d[i]!=b.d[i]) return a.d[i]1&&c.d[c.d[0]]==0) c.d[0]--; return c; } integer operator *(const integer &a,const integer &b)//同类相乘 { integer c; c.d[0]=a.d[0]+b.d[0]; int i,j,x=0; for (i=1; i<=a.d[0]; i++) { x=0; for (j=1; j<=b.d[0]; j++) { x=a.d[i]*b.d[j]+x+c.d[i+j-1]; c.d[i+j-1]=x%radix; x/=radix; } c.d[i+b.d[0]]=x; } while(c.d[0]>1&&c.d[c.d[0]]==0) c.d[0]--; return c; } integer operator *(const integer &a,const long long &k)//类乘long long { integer c; c.d[0]=a.d[0]; int i; long long x=0; for (i=1; i<=a.d[0]; i++) { x+=a.d[i]*k; c.d[i]=x%radix; x/=radix; } while(x>0) { c.d[++c.d[0]]=x%radix; x/=radix; } while(c.d[0]>1&&c.d[c.d[0]]==0) c.d[0]--; return c; } long long rem; integer operator /(const integer &a,const long long &k)//类除long long { integer c; c.d[0]=a.d[0]; long long x=0; for (int i=a.d[0]; i>=1; i--) { x+=a.d[i]; c.d[i]=x/k; x%=k; rem=x; x*=radix; } while(c.d[0]>1&&c.d[c.d[0]]==0) c.d[0]--; return c; } bool smaller(const integer &a,const integer &b,int delta) { if (a.d[0]+delta!=b.d[0]) return a.d[0]+delta0; i--) if (a.d[i]!=b.d[i+delta]) return a.d[i]1&&a.d[a.d[0]]==0) a.d[0]--; } integer operator /(const integer &a,const integer &b)//同类相除 { integer c; d=a; int i,j,temp; mid1[0]=b; for (i=1; i<=13; i++) mid1[i]=mid1[i-1]*2; for (i=a.d[0]-b.d[0]; i>=0; i--) { temp=8192; for (j=13; j>=0; j--) { if (smaller(mid1[j],d,i)) { Minus(d,mid1[j],i); c.d[i+1]+=temp; } temp/=2; } } c.d[0]=max(1,a.d[0]-b.d[0]+1); while(c.d[0]>1&&c.d[c.d[0]]==0) c.d[0]--; return c; } bool operator ==(const integer &a,const integer &b)//判断相等 { if (a.d[0]!=b.d[0]) return 0; for (int i=1; i<=a.d[0]; i++) if (a.d[i]!=b.d[i]) return 0; return 1; } long long q,p; int main () { int T; scanf("%d",&T); while(T--) { scanf("%I64d%I64d",&q,&p); integer Q(q),P(p); integer sum=((Q-2LL)*(Q-1LL)/(2LL)); sum=sum-(sum/P)*P; sum.output(); } }