2017 Multi-University Training 2 解题报告

Is Derek lying?

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1003    Accepted Submission(s): 548


Problem Description
Derek and Alfia are good friends.Derek is Chinese,and Alfia is Austrian.This summer holiday,they both participate in the summer camp of Borussia Dortmund.During the summer camp,there will be fan tests at intervals.The test consists of N choice questions and each question is followed by three choices marked “A ” “B ” and “C ”.Each question has only one correct answer and each question is worth 1 point.It means that if your answer for this question is right,you can get 1 point.The total score of a person is the sum of marks for all questions.When the test is over,the computer will tell Derek the total score of him and Alfia .Then Alfia will ask Derek the total score of her and he will tell her: “My total score is X ,your total score is Y .”But Derek is naughty,sometimes he may lie to her. Here give you the answer that Derek and Alfia made,you should judge whether Derek is lying.If there exists a set of standard answer satisfy the total score that Derek said,you can consider he is not lying,otherwise he is lying.
 
Input
The first line consists of an integer T ,represents the number of test cases.

For each test case,there will be three lines.

The first line consists of three integers N ,X ,Y ,the meaning is mentioned above.

The second line consists of N characters,each character is “A ” “B ” or “C ”,which represents the answer of Derek for each question.

The third line consists of N characters,the same form as the second line,which represents the answer of Alfia for each question.

Data Range:1N80000 ,0X,YN, Ti=1N300000
 
Output
For each test case,the output will be only a line.

Please print “Lying ” if you can make sure that Derek is lying,otherwise please print “Not lying ”.
 
Sample Input
2 3 1 3 AAA ABC 5 5 0 ABCBC ACBCB
 
Sample Output
Not lying Lying
 

这题是签到题。把两者较小的数称为基数,较大数和较小数之间存在分差。对于两者答案相同的题目,可以给两者的基数+1或者不加,两者答案不同的题目,可以给两者产生1分的分差或者不产生。但是注意答案不相同的题目每两个可以使基数+1。因此本题先去除产生分差的答案所需的不相同的题目数,若不够则laying。剩下的答案不相同的题目数/2加上答案相同的题目数若比基数大则可以产生该基数,若不够则laying。
 1 #include<bits/stdc++.h>
 2 #define clr(x) memset(x,0,sizeof(x))
 3 #define clrmax(x) memset(x,0,sizeof(x))
 4 #define LL long long
 5 #define mod 1000000007
 6 using namespace std;
 7 char s1[80010],s2[80010];
 8 int min(int a,int b)
 9 {
10     return a<b?a:b;
11 }
12 int main()
13 {
14     int T,same,dif,x,y,n;
15     scanf("%d",&T);
16     for(int kase=1;kase<=T;kase++)
17     {
18         scanf("%d%d%d",&n,&x,&y);
19         cin>>s1;
20         cin>>s2;
21         same=0;
22         for(int i=0;i<n;i++)
23             if(s1[i]==s2[i])
24                 same++;
25         dif=n-same;
26         if(dif>=abs(x-y) && (dif-abs(x-y))/2+same>=min(x,y))
27             printf("Not lying
");
28         else
29             printf("Lying
");
30     }
31     return 0;
32 }
View Code

Maximum Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1405    Accepted Submission(s): 649


Problem Description
Steph is extremely obsessed with “sequence problems” that are usually seen on magazines: Given the sequence 11, 23, 30, 35, what is the next number? Steph always finds them too easy for such a genius like himself until one day Klay comes up with a problem and ask him about it.

Given two integer sequences {ai} and {bi} with the same length n, you are to find the next n numbers of {ai}: an+1a2n . Just like always, there are some restrictions on an+1a2n : for each number ai , you must choose a number bk from {bi}, and it must satisfy ai ≤max{aj -j│bk ≤j<i}, and any bk can’t be chosen more than once. Apparently, there are a great many possibilities, so you are required to find max{2nn+1ai } modulo 109 +7 .

Now Steph finds it too hard to solve the problem, please help him.
 
Input
The input contains no more than 20 test cases.
For each test case, the first line consists of one integer n. The next line consists of n integers representing {ai}. And the third line consists of n integers representing {bi}.
1≤n≤250000, n≤a_i≤1500000, 1≤b_i≤n.
 
Output
For each test case, print the answer on one line: max{2nn+1ai } modulo 109 +7。
 
Sample Input
4 8 11 8 5 3 1 4 2
 
Sample Output
27
Hint
For the first sample: 1. Choose 2 from {bi}, then a_2…a_4 are available for a_5, and you can let a_5=a_2-2=9; 2. Choose 1 from {bi}, then a_1…a_5 are available for a_6, and you can let a_6=a_2-2=9;
 
Source
 

也是签到题。很容易想到的贪心:每次我们取当前数列中能取的数减去其位置的结果,最大的一个放到数列后面作为该位置的值。因此我们维护一个大根堆,以数-位置作为关键字,存放可取的数以及该数所在的位置。维护一个二叉搜索树,动态存取我们可以取的bi。大根堆用stl的优先队列,二叉搜索树用stl的multiset替代。那么每次加入ai时我们检查堆首的位置在multiset中有无小于他的数,有就贪心地取最近的一个作为该位置使用的bj并从multiset中去除该bi。并将该数作为该位置的值,将数-其位置 以及他的位置入堆;若没有则pop堆首直到有这样的数出现。
 1 #include<bits/stdc++.h>
 2 #define clr(x) memset(x,0,sizeof(x))
 3 #define clrmax(x) memset(x,0,sizeof(x))
 4 #define LL long long
 5 #define mod 1000000007
 6 using namespace std;
 7 struct node
 8 {
 9     int pos,val;
10     bool operator < (const node &b) const
11     {
12         return val<b.val;
13     }
14 }u,v;
15 priority_queue<node> sta;
16 int n,m,t,pos,p;
17 int b[300010];
18 multiset<int>::iterator it;
19 multiset<int> posi;
20 LL ans;
21 int main()
22 {
23     while(scanf("%d",&n)!=EOF)
24     {
25         while(!sta.empty())
26             sta.pop();
27         posi.erase(posi.begin(),posi.end());
28         for(int i=1;i<=n;i++)
29         {
30             scanf("%d",&t);
31             sta.push((node){i,t-i});
32         }
33         for(int i=1;i<=n;i++)
34         {
35             scanf("%d",&t);
36             posi.insert(t);
37         }
38         ans=0;
39         for(int i=1;i<=n;i++)
40         {
41             while((it=posi.upper_bound(sta.top().pos))==posi.begin())
42                 sta.pop();
43             it--;
44             ans=(ans+(LL)sta.top().val)%mod;
45             pos=i+n;
46             t=sta.top().val-pos;
47             sta.push((node){pos,t});
48             posi.erase(it);
49         }
50         printf("%lld
",ans);
51 
52     }
53     return 0;
54 }
View Code

Puzzle

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 271    Accepted Submission(s): 147


Problem Description
A Jigsaw puzzle contains N*M-1 pieces of jigsaws in a N rows*M columns rectangular board.Each jigsaw has a distinct number from 1 to N*M-1.Li is a naughty boy,he wants to arrange the board in his unique way.At the beginning,he picks all N*M-1 jigsaws out and put them on the table and then he will put them back to the board respecting the following steps:
1.Sorting all the remaining jigsaws on the table in ascending order.
2.Picking out the 1st ,the P+1 th ,the 2*P+1 th,......the n*P+1 th jigsaws and put them back to the blank area in the board one by one from the top row to the bottom row,from the left column to the right column.
3.if there are jigsaws remained on the table,back to step 1.
After he arranging the board,it’s obvious that there’s only one blank area located at the bottom-right corner.
Your task is to make the numbers on jigsaws sorted with every row and every column in ascending order(From left to right,top to bottom),and the blank area should be located at the bottom-right corner in the end.Each step you can move the blank area’s neighboring jigsaws(which share a common side with the blank area) towards the blank area.It’s really a difficult question,so you need to write a program to judge whether it is possible to complete the task.



 
Input
The first line contains an integer T(T<=100),which represents the number of test cases.
Following T lines,each line contains three integers N,M,P(2<=N,M<=1000;1<=P<=N*M-2).
 
Output
For each test case,print “YES” in a separate line if it is possible to complete the task ,otherwise please print “NO”.
 
Sample Input
3 3 2 3 3 2 4 999 999 1
 
Sample Output
YES NO YES
 
这题的话他希望我们移完能成为一个杨氏矩阵,并且所有数不重复且为1~n*m-1。做过八数码的同学都知道两个矩阵要通过移动一个空格来互相达到,那么他们按行(或列)把列(或行)排列起来形成的一个数列他们的逆序数对数的奇偶性要相同。对于题目所述的杨氏矩阵,可以证明只有逆序数对数为偶数的杨氏矩阵才有可能由题目的给的初始矩阵达到。因此我们算下题目中给的矩阵的逆序数对数,偶数则可达,奇数则不可达。按题意生成原始排列,观察发现,每一轮数后方比该数小的数的数量(即对逆序对数的贡献)呈等差数列形式,公差p-1,项数为(num-1)/p+1,照此简化计算,不需要正真求出排列。
 1 #include<bits/stdc++.h>
 2 #define clr(x) memset(x,0,sizeof(x))
 3 using namespace std;
 4 int main()
 5 {
 6     int T;
 7     int n,m,p,q,t,ans,tot;
 8     scanf("%d",&T);
 9     while(T--)
10     {
11         ans=0;
12         scanf("%d%d%d",&n,&m,&p);
13         tot=n*m-1;
14         while(tot>p)
15         {
16             t=(tot-1)/p+1;
17             ans+=(p-1)*t*(t-1)/2;
18             tot-=t;
19         }
20         if(ans%2)
21             printf("NO
");
22         else
23             printf("YES
");
24     }
25     return 0;
26 }
View Code

Sdjpx Is Happy

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 326    Accepted Submission(s): 124


Problem Description
Sdjpx is a powful man,he controls a big country.There are n soldiers numbered 1~n(1<=n<=3000).But there is a big problem for him.He wants soldiers sorted in increasing order.He find a way to sort,but there three rules to obey.
1.He can divides soldiers into K disjoint non-empty subarrays.
2.He can sort a subarray many times untill a subarray is sorted in increasing order.
3.He can choose just two subarrays and change thier positions between themselves.
Consider A = [1 5 4 3 2] and P = 2. A possible soldiers into K = 4 disjoint subarrays is:A1 = [1],A2 = [5],A3 = [4],A4 = [3 2],After Sorting Each Subarray:A1 = [1],A2 = [5],A3 = [4],A4 = [2 3],After swapping A4 and A2:A1 = [1],A2 = [2 3],A3 = [4],A4 = [5].
But he wants to know for a fixed permutation ,what is the the maximum number of K?
Notice: every soldier has a distinct number from 1~n.There are no more than 10 cases in the input.
 
Input
First line is the number of cases.
For every case:
Next line is n.
Next line is the number for the n soildiers.
 
Output
the maximum number of K.
Every case a line.
 
Sample Input
2 5 1 5 4 3 2 5 4 5 1 2 3
 
Sample Output
4 2
Hint
Test1: Same as walk through in the statement. Test2: [4 5] [1 2 3] Swap the 2 blocks: [1 2 3] [4 5].
 
题解从略,代码中有注释。
 1 #include<bits/stdc++.h>
 2 #define clr(x) memset(x,0,sizeof(x))
 3 #define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x))
 4 #define LL long long
 5 #define mod 1000000007
 6 #define N 3010
 7 using namespace std;
 8 int f[N][N],minx[N][N],maxx[N][N],last[N],num[N];
 9 int n,m,k;
10 inline int max(int a,int b)
11 {
12     return a>b?a:b;
13 }
14 inline int min(int a,int b)
15 {
16     return a<b?a:b;
17 }
18 void predeal(int n)
19 {
20     for(int i=1;i<=n;i++)
21     {
22         last[i]=i;
23         f[i][i]=1;
24         minx[i][i]=maxx[i][i]=num[i];
25         for(int j=i+1;j<=n;j++)
26         {
27             minx[i][j]=min(minx[i][j-1],num[j]);
28             maxx[i][j]=max(maxx[i][j-1],num[j]);
29         }
30     }
31     int j;
32     for(int l=1;l<n;l++)
33     {
34         for(int i=1;i<=n-l;i++)
35         {
36             j=i+l;
37             if(maxx[i][j]-minx[i][j]!=j-i)
38                 f[i][j]=0;
39             else
40             {
41                 if(minx[i][j]<minx[i][last[i]])
42                     f[i][j]=1;
43                 else
44                     f[i][j]=f[i][last[i]]+f[last[i]+1][j];
45                 last[i]=j;
46             }
47 //            cout<<i<<" "<<j<<":"<<f[i][j]<<endl;
48         }
49     }
50     return ;
51 }
52 int main()
53 {
54     int T,p,ans;
55     scanf("%d",&T);
56     while(T--)
57     {
58         scanf("%d",&n);
59         for(int i=1;i<=n;i++)
60             scanf("%d",&num[i]);
61         predeal(n);//处理n^2确定i~j间不用交换情况下最多分几组
62         //n^2暴力,确定要交换的靠前的区间后,就能确定靠后区间的右端点,判断是否合法,合法则枚举靠后区间的左端点看是否能合法与靠前区间交换。合法则计算其分组数。在这些分组中和不交换情况下取最大值即为答案。
63         ans=0;
64         for(int i=1;i<=n;i++)
65             for(int j=i;j<=n;j++)
66                 if(f[i][j] && (i==1||(f[1][i-1] && minx[1][j-1]==1)))
67                 {
68                     p=maxx[i][j];
69                     if(p==n || (f[p+1][n] && maxx[p+1][n]==n))
70                     {
71                         for(int k=p;k>j;k--)
72                         {
73                             if(f[k][p] && minx[k][p]==i)
74                             {
75 //                                cout<<i<<" "<<j<<" "<<k<<" "<<p<<":"<<f[1][i-1]+(k-1>=j+1?f[j+1][k-1]:0)+f[p+1][n]+2<<endl;
76                                 ans=max(ans,f[1][i-1]+(k-1>=j+1?f[j+1][k-1]:0)+f[p+1][n]+2);
77                             }
78                         }
79                     }
80                 }
81         ans=max(ans,f[1][n]);
82         printf("%d
",ans);
83     }
84     return 0;
85 }
View Code

Funny Function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1016    Accepted Submission(s): 491


Problem Description
Function Fx,y satisfies:

For given integers N and M,calculate Fm,1 modulo 1e9+7.
 
Input
There is one integer T in the first line.
The next T lines,each line includes two integers N and M .
1<=T<=10000,1<=N,M<2^63.
 
Output
For each given N and M,print the answer in a single line.
 
Sample Input
2 2 2 3 3
 
Sample Output
2 33
 
 
我们用特征方程求解第一列的通项,可得 ( a_n= frac{1}{3} ( 2^n + {(-1)}^{n+1})) 按照该式进行等比数列求和得 ( S_n ) ,然后可得第二列 ( b_n= frac{1}{3} [ (2^N-1 ) 2^n +frac{1-{(-1)}^N}{2}  {(-1)}^{n+1}]  ),可得第二列通项比第一列通项多出的了 ( (2^N-1 ) ) 和 ( frac{1-{(-1)}^N}{2} )。那我们再往后对该通项求和依旧会多出这两个数分别乘这俩两个等比数列。因此可以推出所有项的通项公式:( F_{i,j} =  frac{1}{3} [ { (2^N-1) }^{i-1} * 2^j +{ ( frac{1-{(-1)}^N}{2} ) }^{i-1}  {(-1)}^{j+1}] ),然后推出 ( F_{m,1}= =  frac{1}{3} [ { (2^N-1) }^{m-1} * 2 +{ ( frac{1-{(-1)}^N}{2}  )}^{m-1}  {(-1)} ] ),后面-1的这一项判个奇偶即可得,前面2的这项写个快速幂求出,两者相加后除以3,由于取模,因此变为对3求逆元,乘该逆元即可得。
公式出错看这里:
 1 #include<bits/stdc++.h>
 2 #define clr(x)  memset(x,0,sizeof(x))
 3 #define mod 1000000007
 4 #define LL long long
 5 using namespace std;
 6 LL n,m,ans;
 7 LL quick_pow(LL i,LL n)
 8 {
 9     LL res=1,mul=(i%mod+mod)%mod;
10     while(n)
11     {
12         if(n%2==1)
13             res=(res*mul)%mod;
14         mul=(mul*mul)%mod;
15         n/=2;
16     }
17     return res;
18 }
19 void exgcd(LL a,LL b,LL &x,LL &y,LL &gcd)
20 {
21     if(!b) {gcd=a; x=1; y=0; }
22     else {exgcd(b,a%b,y,x,gcd); y-=x*(a/b); }
23     return ;
24 }
25 int main()
26 {
27     int T;
28     LL x,y,gcd;
29     scanf("%d",&T);
30     for(int kase=1;kase<=T;kase++)
31     {
32         scanf("%lld%lld",&n,&m);
33         ans=(quick_pow((quick_pow(2,n)-1),m-1)*2%mod+(n%2==1?1:0)+mod)%mod;
34         exgcd(3,mod,x,y,gcd);
35         x=(x%mod+mod)%mod;
36         ans=(ans*(LL)x)%mod;
37         printf("%lld
",ans);
38     }
39     return 0;
40 }
View Code

To my boyfriend

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 630    Accepted Submission(s): 289


Problem Description
Dear Liao

I never forget the moment I met with you. You carefully asked me: "I have a very difficult problem. Can you teach me?". I replied with a smile, "of course". You replied:"Given a matrix, I randomly choose a sub-matrix, what is the expectation of the number of **different numbers** it contains?"

Sincerely yours,
Guo
 
Input
The first line of input contains an integer T(T≤8) indicating the number of test cases.
Each case contains two integers, n and m (1≤n, m≤100), the number of rows and the number of columns in the grid, respectively.
The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_i,j (0≤ g_i,j < n*m).
 
Output
Each case outputs a number that holds 9 decimal places.
 
Sample Input
1 2 3 1 2 1 2 1 2
 
Sample Output
1.666666667
Hint
6(size = 1) + 14(size = 2) + 4(size = 3) + 4(size = 4) + 2(size = 6) = 30 / 18 = 6(size = 1) + 7(size = 2) + 2(size = 3) + 2(size = 4) + 1(size = 6)
 
 
我们不从矩阵,而从某个数字来看他对答案的贡献。对于每个数字,在每个矩阵中最上左的该数字为有效贡献的数字(先上后左)。因此我们对每个格子检查他所对应的数字对答案的贡献。做这个需要O(n)从列的位置往左和右建立两个单调栈,然后两个单调栈互弹求解,加起来是一个O(n^3)的做法。
 1 #include<bits/stdc++.h>
 2 #define clr(x)  memset(x,0,sizeof(x))
 3 #define LL long long
 4 using namespace std;
 5 int maped[110][110];
 6 int last[10010][110],inlast[110];
 7 long double ans;
 8 stack< pair<int,int> > pushlas,by;
 9 int max(int a,int b)
10 {
11     return a>b?a:b;
12 }
13 int main()
14 {
15     int T,n,m,t,color,inlas,ct;
16     LL ans;
17     scanf("%d",&T);
18     for(int kase=1;kase<=T;kase++)
19     {
20         scanf("%d%d",&n,&m);
21         for(int i=1;i<=n;i++)
22             for(int j=1;j<=m;j++)
23                 scanf("%d",&maped[i][j]);
24         clr(last);
25         ans=0;
26         for(int i=1;i<=n;i++)
27             for(int j=1;j<=m;j++)
28             {
29                 t=0;
30                 inlas=0;
31                 color=maped[i][j];
32                 for(int k=m;k>=j;k--)
33                 {
34                     while(!by.empty() && last[color][k]>=by.top().first)
35                         by.pop();
36                     by.push(make_pair(last[color][k],k));
37                 }
38                 while(!by.empty())
39                 {
40                     pushlas.push(by.top());
41                     by.pop();
42                 }
43                 inlas=0;
44                 for(int k=j;k>=1;k--)
45                 {
46                     inlas=max(inlas,last[color][k]);
47                     inlast[k]=inlas;
48                 }
49                 inlas=m+1;
50                 ct=0;
51                 for(int k=1;k<=j;k++)
52                 {
53                     while(pushlas.top().first>inlast[k])
54                     {
55                         ans+=(LL)(ct+(i-pushlas.top().first)*(j-k+1))*(LL)(inlas-pushlas.top().second)*(LL)(n-i+1);
56                         inlas=pushlas.top().second;
57                         pushlas.pop();
58                     }
59                     ct+=i-inlast[k];
60                 }
61                 ans+=(LL)ct*(inlas-pushlas.top().second)*(LL)(n-i+1);
62                 pushlas.pop();
63                 last[color][j]=i;
64             }
65         printf("%0.9lf
",(double)ans/((n+1)*n*(m+1)*m)*4);
66     }
67     return 0;
68 }
View Code

TrickGCD

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1996    Accepted Submission(s): 778


Problem Description
You are given an array A , and Zhu wants to know there are how many different array B satisfy the following conditions?

* 1BiAi
* For each pair( l , r ) (1lrn ) , gcd(bl,bl+1...br)2
 
Input
The first line is an integer T(1T10 ) describe the number of test cases.

Each test case begins with an integer number n describe the size of array A .

Then a line contains n numbers describe each element of A

You can assume that 1n,Ai105
 
Output
For the k th test case , first output "Case #k: " , then output an integer as answer in a single line . because the answer may be large , so you are only need to output answer mod 109+7
 
Sample Input
1 4 4 4 4 4
 
Sample Output
Case #1: 17
 

这题用莫比乌斯反演做这是毋庸置疑的。枚举每个除数d,求出每个ai可取的个数 即 ai/d,然后相乘即为该除数下(gcd不一定为该除数)的方案数。然后通过容斥加加减减(也就是莫比乌斯反演)就能得出最后的结果。这样做的时间复杂度是极高的,为O(n^2),铁定超时。我们需要对相乘这个部分做点优化。我们枚举每个除数时,对ai/d相同的部分分块,并用快速幂求解该块的贡献然后相乘,这样能使时间复杂度大大下降。
 1 #include<bits/stdc++.h>
 2 #define clr(x) memset(x,0,sizeof(x))
 3 #define clrmax(x) memset(x,0,sizeof(x))
 4 #define LL long long
 5 #define mod 1000000007
 6 using namespace std;
 7 int inf[100050],mu[100050];
 8 int prime[100050];
 9 int a[100050],sum[100050];
10 int n,m,pcnt,ct,minai,maxai;
11 LL ans,sing;
12 void  tooprimer(int n)
13 {
14     clr(inf);
15     pcnt=0;
16     for(int i=2;i<=n;i++)
17     {
18         if(!inf[i])
19         {
20             prime[++pcnt]=i;
21             inf[i]=1;
22             mu[i]=1;
23         }
24         for(int j=1;j<=pcnt;j++)
25         {
26             if(i*prime[j]>n) break;
27             inf[i*prime[j]]=1;
28             if(i%prime[j]==0)
29             {
30                 mu[i*prime[j]]=0;
31                 break;
32             }
33             else
34                 mu[i*prime[j]]=-mu[i];
35         }
36     }
37     return ;
38 }
39 LL quick_pow(LL i,LL n)
40 {
41     LL res=1,mul=(LL)i;
42     while(n)
43     {
44         if(n&1)
45             res=(res*mul)%mod;
46         mul=(mul*mul)%mod;
47         n>>=1;
48     }
49     return res;
50 }
51 
52 inline int min(int &a,int &b)
53 {
54     return a<b?a:b;
55 }
56 inline int max(int &a,int &b)
57 {
58     return a>b?a:b;
59 }
60 int main()
61 {
62     tooprimer(100000);
63     int T,sqr;
64     scanf("%d",&T);
65     for(int kase=1;kase<=T;kase++)
66     {
67         minai=0x3f3f3f3f;
68         maxai=0;
69         scanf("%d",&n);
70         clr(sum);
71         for(int i=1;i<=n;i++)
72         {
73             scanf("%d",&a[i]);
74             sum[a[i]]++;
75             minai=min(a[i],minai);
76             maxai=max(a[i],maxai);
77         }
78         for(int i=1;i<=maxai;i++)
79             sum[i]=sum[i-1]+sum[i];
80         ans=0;
81         for(int i=2;i<=maxai;i++)
82         {
83             sing=1;
84             if(mu[i]==0)
85                 continue;
86             if(sum[i-1]>0)
87                 continue;
88             for(int j=i;j<=maxai;j+=i)
89             {
90                 sing=sing*quick_pow((LL)(j/i),(LL)(sum[(j+i-1>maxai?maxai:j+i-1)]-sum[j-1]))%mod;
91             }
92             ans=((ans+sing*(LL)mu[i])%mod+mod)%mod;
93         }
94         printf("Case #%d: %lld
",kase,ans);
95     }
96     return 0;
97 }
View Code

Regular polygon

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1959    Accepted Submission(s): 776


Problem Description
On a two-dimensional plane, give you n integer points. Your task is to figure out how many different regular polygon these points can make.
 
Input
The input file consists of several test cases. Each case the first line is a numbers N (N <= 500). The next N lines ,each line contain two number Xi and Yi(-100 <= xi,yi <= 100), means the points’ position.(the data assures no two points share the same position.)
 
Output
For each case, output a number means how many different regular polygon these points can make.
 
Sample Input
4 0 0 0 1 1 0 1 1 6 0 0 0 1 1 0 1 1 2 0 2 1
 
Sample Output
1 2

整数点对应的正多边形只有正方形。看透这点这题就是水题了。枚举所有边求有没有对应的正方形即可。记得去除重复的正方形。
 1 #include<bits/stdc++.h>
 2 #define clr(x) memset(x,0,sizeof(x))
 3 #define clrmax(x) memset(x,0,sizeof(x))
 4 #define LL long long
 5 #define mod 1000000007
 6 #define come 210
 7 using namespace std;
 8 bool mapped[800][800];
 9 struct node
10 {
11     int x,y;
12 }pt[1000];
13 int main()
14 {
15     int T,n,m,k,l,maxx,ct,xi,yi,lenk,lenl,t,ans;
16     LL p;
17     while(scanf("%d",&n)!=EOF)
18     {
19         clr(mapped);
20         for(int i=1;i<=n;i++)
21         {
22             scanf("%d%d",&pt[i].x,&pt[i].y);
23             pt[i].x+=come;
24             pt[i].y+=come;
25             mapped[pt[i].x][pt[i].y]=1;
26         }
27         ans=0;
28         for(int i=1;i<=n;i++)
29             for(int j=1;j<=n;j++)
30                 if(i!=j)
31                 {
32                     yi=-(pt[i].x-pt[j].x);
33                     xi=pt[i].y-pt[j].y;
34                     if(xi+pt[i].x>0 && yi+pt[i].y>0 && mapped[xi+pt[i].x][yi+pt[i].y]==1 && xi+pt[j].x>0 && yi+pt[j].y>0 && mapped[xi+pt[j].x][yi+pt[j].y]==1)
35                         ans++;
36                     xi=-xi;
37                     yi=-yi;
38                     if(xi+pt[i].x>0 && yi+pt[i].y>0 && mapped[xi+pt[i].x][yi+pt[i].y]==1 && xi+pt[j].x>0 && yi+pt[j].y>0 && mapped[xi+pt[j].x][yi+pt[j].y]==1)
39                         ans++;
40                 }
41         printf("%d
",ans/8);
42     }
43     return 0;
44 }
View Code
原文地址:https://www.cnblogs.com/wujiechao/p/7256162.html