#P7893. 『JROI-3』Reversi

『JROI-3』Reversi

Description

White is playing Reversi with a forest spirit race, but the rules of Reversi have been changed.

There are nn Reversi pieces. The ii-th piece is labeled ii. All pieces are initially black. During the game, only White operates. White wants to turn as many pieces as possible to white.

White requires that piece kk and piece k×pk \times p cannot both be turned white at the same time.

White played TT games in total. In each game, White wants to know the maximum number of pieces that can be turned white. Each game is independent.

To avoid confusion, the bold White is a person’s name.

Input Format

The first line contains a positive integer TT, denoting the number of test cases.

The next TT lines each contain two integers n,pn, p, as described.

Output Format

Output TT lines. Each line contains one positive integer, denoting the maximum number of pieces that White can turn white.

1
3 2
2
1
100 5
84

Hint

Sample 1 Explanation

You can choose pieces 2,32, 3 to change color.

Constraints

This problem uses bundled tests.

  • Subtask 1 (5 pts): T5T \le 5, n2n \le 2;
  • Subtask 2 (5 pts): T5T \le 5, n10n \le 10;
  • Subtask 3 (20 pts): T5T \le 5, n106n \le 10^6;
  • Subtask 4 (70 pts): no special constraints.

For 100%100\% of the testdata, 1T1061 \le T \le 10^6, 0n10180 \le n \le 10^{18}, 1p1091 \le p \le 10^{9}.

// Fast input template
// During-contest reminder: fast input is not very necessary
inline long long read(){
   long long s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
   return s*w;
}

Translated by ChatGPT 5