【zzulioj 1893 985的数学难题】
来源:互联网 发布:java long保留两位小数 编辑:程序博客网 时间:2024/04/30 05:51
985的数学难题
Description
985有n个正整数,他想快速知道下面函数的返回值
int a[N+1];
long long Solve() {
int i, j;
long long ans = 0;
for(i = 1; i <= N; i++) {
for(int j = i + 1; j <= N; j++) {
ans += a[i] + a[j] + (a[i] ^ a[j]) + (a[i] | a[j]) + (a[i] & a[j]);
}
}
return ans;
}
注:^表示异或运算。
Input
第一行输入一个整数t,代表有t组测试数据。
每组数据第一行输入一个整数代表元素个数,接下来一行输入n个正整数a[]。
注:1 <= t <= 30,1 <= n,a[] <= 100000。
Output
一个整数代表最后的返回值ans。
Sample Input
2
1
10
2
1 1
Sample Output
0
4
#include<stack>#include<cmath>#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define K 100011#define INF 0x3f3f3fusing namespace std;bool cmp(int a,int b){ return a>b;}long long pa[K];int main(){ int i,j; int N; int T; long long ans; long long ml; long long cot; scanf("%d",&T); while(T--) { scanf("%d",&N); ans=0; for(i=1;i<=N;i++) { scanf("%lld",&pa[i]); ans+=pa[i]; } ans*=(N-1); //先把和求出,每个数相加时都用到了(N-1)次 ; sort(pa+1,pa+N+1,cmp); ml=1; //做为每次尾数更新时的见证 while(pa[1]) //当最大的数小于 1 时跳出循环 { cot=0; //初始化尾数为 1 的个数 for(i=1;i<=N;i++) { if(pa[i]==0) //若a[i]为零,则剩下的数定全为零 break; if(pa[i]&1) //每次只判断尾数是否为为 1 cot++; pa[i]>>=1; //更新尾数 } ans+=(cot*(cot-1)>>1)*ml; //只有当两个数的尾数同时才有贡献,从尾数为1的数里挑出两个,总数为(cot*(n-cot)/2); ans+=(cot*(N-cot)+(cot*(cot-1)>>1))*ml; //当一个数的尾数为 1 时就有贡献,先从尾数为1和0的数里各挑出一个,然后从尾数为 1 的数里挑出两个 ans+=(cot*(N-cot))*ml; // 只有当尾数不同时才有贡献,从尾数为1和0的个数里各挑出一个,总数为(cot*(n-cot)); ml<<=1; //更新每次 ml 的值,尾数每更新一次,ml左移一位; } printf("%lld\n",ans); } return 0;}
0 0
- 【zzulioj 1893 985的数学难题】
- 【zzuliOJ】1893 - 985的数学难题(位运算)
- ZZULIOJ Problem 1893: 985的数学难题 【位运算】
- 【zzuliOJ】1894 - 985的方格难题(组合数学)
- 【多校训练】ZZULIOJ 1893 985的数学难题【位运算】
- ZZULIOJ/ 郑轻OJ 1895: 985的0-1串难题(数学思维)
- 1893: 985的数学难题
- ZZULIOJ 1898: 985的数字难题 【水题】
- 【zzulioj 1898 985的数字难题】
- 【zzulioj 1896 985的买饮料难题 】
- 【zzulioj 1897 985的红绿灯难题】
- 【zzulioj 1899 985的最大和难题】
- zzuliOJ 1894: 985的方格难题 【dp】
- zzulioj 1899(985的最大和难题)
- zzulioj 1894: 985的方格难题
- zzulioj 1896 985的买饮料难题
- zzulioj 1897 985的红绿灯难题
- zzulioj 1898 985的数字难题
- Hdu 5813 Elegant Construction【贪心】
- 巧用sprintf()构造mysql语句
- Linux用户身份切换
- 【51Nod】1085 - 背包问题(01背包)
- PHP假期复习(三)---http协议
- 【zzulioj 1893 985的数学难题】
- java和页面中文乱码问题
- IntelliJ IDEA 设置代码提示或自动补全的快捷键
- Find Leaves of Binary Tree
- 字符串 KMP HDU 1686
- 需要认识的c math 函数
- PHP假期复习(四)---web技术
- 运算符重载
- hdu 5818 Joint Stacks 栈