codechef April challenge (2)(学习去优化提高效率)
来源:互联网 发布:python去除重复列 编辑:程序博客网 时间:2024/06/05 06:04
Counting Matrices
Problem code: CNPIIM
此题逆向解决问题,先求出乘积为i的数对有多少个,到询问的时候直接输出就行了!
Lira loves Linear Algebra and she is especially keen about matrix :).
Today, she learnt some properties about matrices, namely, she learnt about what the trace of a matrix is, as her teacher gave her many exercises for her to practice.
As we know she is pretty clever, she rapidly came up with some definitions of her own and devised a somewhat harder version of the problem initially proposed by her teacher.
Namely, she defines a Positive Invertible Integer Matrix as being an invertible 2x2 matrix composed only of positive (i.e. greater than 0) integer elements and whose determinant is greater than 0.
Now, she is interested in counting how many such matrices are there, such that their trace is equal toN .
It's your turn to solve Lira's problem :D
Input
The first line of the input contains an integer T denoting the number of test cases. The description ofT test cases follows.
Each test case consist of single integer N, the trace of the matrix.
Output
For each test case, output a single line containing the number of Positive Invertible Integer Matrices such that their trace is equal toN and dterminant is positive.
Constraints:
- 1 ≤ T ≤ 50
- 3 ≤ N ≤ 2500
Example
Input:13Output:2
Explanation
The only two matrices that exist with trace equal to 3 and that satisfy all the given conditions are:
#include<stdio.h>#define N 2500const int MAX=N*N/4;int dp[MAX],num[MAX];//计算乘积小于x的数对的个数void unit(){ //计算乘积为i的数对的对数,逆向得到存储在数组里 int i; for(i=1;i<=MAX;i++) { for(int j=1;j<=MAX;j++) { if(i*j>MAX) break; dp[i*j]++; } } for(i=1;i<MAX;i++) { num[i]=num[i-1]+dp[i]; }}int main(){ unit(); int a1,t,n; scanf("%d",&t); for(;t>0;t--) { long long sum=0; scanf("%d",&n); for(a1=1;a1<=(n-1)/2;a1++) { int tri=a1*(n-a1); sum+=num[tri-1]; } if(n%2!=0) printf("%lld\n",2*sum); else printf("%lld\n",sum*2+num[n/2*n/2-1]); } return 0;}
- codechef April challenge (2)(学习去优化提高效率)
- codechef April challenge (1)(学习去优化时间效率???)
- Codechef April Challenge 2017
- [题解]CodeChef APRIL Challenge 17
- codechef:April Challenge 2014: Cards, bags and coins
- 【codechef】Arush Challenge(灵活题)
- CodeChef - CDSW153 Virat and his challenge(容斥原理)
- May Challenge 2015(CodeChef 2015年5月月赛)
- [最短路] [CODECHEF April Challenge 2017] 小熊与团间距离(CLIQUED)
- [CodeChef FEB14]Graph Challenge解题报告(求半支配点)
- BSOJ4835刷漆(Codechef October Challenge 2014:Remy paints the fence)
- codechef December Challenge 2012
- codechef December Challenge 2012
- codechef December Challenge 2012
- codechef May challenge A
- codechef May challenge B
- codechef May challenge C
- CodeChef May Challenge 2014
- 还原恢复Windows Server 2003 SP2 /R2的默认权限设置
- 理解.NET 2.0中的事务
- Cocos2dx:菜单(CCMenu,CCMenuItem)
- 向量类的加减运算和比较
- 利用某一列的值修改之后来更新其自己
- codechef April challenge (2)(学习去优化提高效率)
- tinyXML使用
- windows 2003安全设置去权限批处理
- Java的HashMap和HashTable
- POJ-2078-Matrix
- 李白喝酒问题
- ubuntu kylin 14.04 64位下进行Android 4.4.2 R2版本的源码编译
- Java之观察者模式
- LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt