Give some positive integer pairs (ni,ci), ZYB wants to know fci(ni)mod(109+7).
输入描述
The input contains multiple test cases. The first line of input contains one integer T(1⩽T⩽106).
In the following T lines, each line contains two integers ni,ci(1⩽ni,ci⩽106) describing one question.
输出描述
For each test case, output one integer indicating the answer.
示例1
输入
1 2 3
2 3 3 10 5
输出
1 2
3 25
分析
题目给出了一个可递归求解的函数,递归停止的条件为:当 x=1,fc(x)=1。递推式为 fc(x)=1⩽i⩽x−1maxcfc(gcd(i,x)),c 可以看作常数,若递归了 n 次,那么就会产生乘子 cn。由于每次递归都取 max,所以递归的次数要尽量多(也就是让乘子尽量大)。最多可以递归 显然,若 fc(x) 最多可以递归 n 次,则 fc(x)=cn,我们要求的就是最大的递归次数。
递归次数显然与 gcd(i,x) 有关。由算数基本定理,当 x>1,可以将 x 写成多个质数的乘积,不妨设 x=p1k1p2k2⋯pmkm,质因子个数为 cntx=i=1∑mki。经过一次递归,就会求一次 gcd,就会损失一定数量的质因子。如果每次递归只损失一个质因子,那么递归次数显然是最多的,为 cntx;这样的方案是存在的,比如说 i=p1k1−1p2k2⋯pmkm。
综上所述,fc(x)=ccntx,cntx 为 x 的质因子数量。
若每组测试数据单独求质因子数量,时间复杂度为 O(Tn),会 TLE。不妨直接用欧拉筛在线性时间内得到 1∼106 内各个整数的质因子数量。若 x 为质数,那么 cntx=1;若 x 为合数,有递推式 cntx×p=cntx+1,其中 p 为质数。
ZYB has a so-called smart brain. He can always point out the key-point in a complex problem.
There are two parallel lines AB and CD in a plane. A,B,C,D are all distinct points.
You only know the Euclidean Distances between AC,AD,BC,BD. But you don’t know the exact order of points (i.e. You don’t know whether it’s AB//CD or AB//DC).
Could you determine the order of points quickly, like the ZYB does?
输入描述
The input contains multiple cases. The first line of the input contains a single integer T(1⩽T⩽100), the number of cases.
For each case, there are four integers a,b,c,d(1⩽a,b,c,d⩽1000) in a line, indicating the distances between AC,AD,BC,BD.
It is guaranteed that each case corresponds to a valid solution.
输出描述
For each case, output AB//CD (Quotation marks) if AB//CD, or output AB//DC (Quotation marks) if AB//DC.
### 代码 ```cpp /****************************************************************** Copyright: 11D_Beyonder All Rights Reserved Author: 11D_Beyonder Problem ID: 2020牛客暑期多校训练营(第四场) Problem F Date: 8/15/2020 Description: Computational Geometry *******************************************************************/ #include<iostream> #include<cstdio> using namespace std; int main(){ int _; for(cin>>_;_;_--){ int AC,AD,BC,BD; scanf("%d%d%d%d",&AC,&AD,&BC,&BD); puts(AD+BC>AC+BD?"AB//CD":"AB//DC"); } return 0; }
After solving the Basic Gcd Problem, ZYB gives you a more difficult one:
Given an integer n, find two subset A and B of $\{1,2,\dots,n\}$ such that: ∣A∣=∣B∣=m and A∩B=∅; let $\mathbb A=\{a_1,a_2,\cdots,a_{m}\}$ and $\mathbb B=\{b_1,b_2,\cdots,b_m\}$, there exists two permutations p1,p2,⋯,pm and q1,q2,⋯,qm such that for each 1⩽i⩽m, gcd(api,bqi)>1.
Please find two subsets with maximum value of m.
输入描述
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases.
For each test case, there is only one line containing an integer n(4⩽n⩽2×105). It’s guaranteed that the sum of n of all test cases will not exceed 2×105.
输出描述
For each test case, output an integer m in the first line. In the next m lines, each contains two integers ai and bi(gcd(ai,bi)>1), denoting the i-th element in subset A and B.
If there are multiple solutions, you can output any of them.
示例1
输入
1 2 3
2 4 10
输出
1 2 3 4 5 6 7
1 2 4 4 3 9 5 10 8 2 4 6
分析
题意描述较为复杂,不妨直接令 p 和 q 都为 1∼n 的标准排列,那么问题转化为:在区间 [1,n] 中取出尽量多的正整数对 (x,y),满足 gcd(x,y)>1,且每个数字只能使用一次;将 x 放入序列 a,y 放入序列 b,序列的长度为 m,∀i∈[1,m],且 i∈N∗,有 gcd(ai,bi)>1。
显然,对于 1∼n 之间的全体偶数,可以随意配对。所以我们暂且不考虑偶数,将偶数放到最后配对。
先枚举除 2 以外所有不超过 n 的质数 p,对于 2p>n 的质数 p,不存在与其配对的数,首先将其剔除。若质数 2p⩽n,那么对于不大于 n 的 p 的倍数,可以组成集合 $\mathbb P=\{x=kp|x\leqslant n,k\in\mathbb N^\ast\}$;显然,若集合 P 中任意两个元素都不互质;若 card(P) 为偶数,那么正好可以两两配对;若 card(P) 为奇数,取出 P 中一个偶数暂时不用(事实上,P 中一定会包含 2p),将剩余元素两两配对。此时,一部分偶数已经配对,如 2p,4p,⋯。
最后,将剩下的还没配对的偶数两两配对。