↓ 跳过正文
  1. Posts/

codeforces #edu 1 A tricky sum

·493 字·1 分钟

题意:求1+2+..+n的和。。但是对于是2的整数幂的项数。。符号是-。。

思路:可以先当做正数。(n+1)*n/2; 然后减去二倍的2的整数次幂的项的和。

坑点: 妈蛋第三次了。。。我想求小于等于n的最大是2的几次幂。。。取整的时候用int又会迷之错误。。。为什么说是迷之错误。。因为我WA的点的数据拿下来在本地跑是没有问题的。。。一交上去就错。。。不明觉厉。。。下次遇到double类型是数一点要小心小心再小心。。。第一次遇到是pow的返回类型是double,然后答案莫名奇妙的差1.第二次是#334 div2 的A题。。一道傻逼算分数的题我WA了一个小时。。。第三次是这个。。向下取整不要用(int)的强制转换。。而用floor吧。。233

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月03日 星期四 16时46分46秒
 4File Name :code/cf/edu/A.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25
26
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33LL n;
34int main()
35{
36	#ifndef  ONLINE_JUDGE
37	freopen("code/in.txt","r",stdin);
38  #endif
39
40	int T;
41	scanf("%d",&T);
42	while (T--)
43	{
44	    LL sum = 0 ;
45	    cin>>n;
46	    LL k = LL(floor(log(n)/log(2)));
47	   // cout<<"k:"<<k<<endl;
48	    k =(1<<(k+1))-1;
49	 //   cout<<"kk:"<<k<<endl;
50	    sum = sum-2*k;
51	  //  cout<<"sum:"<<sum<<endl;
52	    s`um = sum + (n+1)*n/2;
53	    cout<<sum<<endl;
54	}
55
56  #ifndef ONLINE_JUDGE
57  fclose(stdin);
58  #endif
59    return 0;
60}

相关文章

codeforces #334 div 2 C. Alternative Thinking

·517 字·2 分钟
题意:给定一个01串。要进行一次变换:选一段连续的非空的字串,将这段串的0和1反转(0变成1,1变成0) 然后问能得到的最长的0,1交替的序列的长度是多少(不一定连续)

codeforces #332 div 2 C. Day at the Beach

·561 字·2 分钟
C. Day at the Beach time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles.

codeforces #332 div 2 B. Spongebob and Joke

·651 字·2 分钟
B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick’s personal stuff and found a sequence _a_1, _a_2, …, a__m of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence _f_1, _f_2, …, f__n of length n and for each number a__i got number b__i = f__a__i. To finish the prank he erased the initial sequence a__i.

codeforces #332 div 2 A. Patrick and Shopping

·646 字·2 分钟
1#include <cstdio> 2#include <iostream> 3#include <cmath> 4using namespace std; 5long long d1,d2,d3; 6int main() 7{ 8cin>>d1>>d2>>d3; 9long long ans = 999999999999; 10ans = min(ans,d1+d2+d3); 11ans = min (ans,d1*2+d2*2); 12ans = min (ans,d1*2+2*d3); 13ans = min(ans,d2*2+2*d3); 14cout<<ans<<endl; 15return 0; 16} time limit per test 1 second

codeforces 589 B - Layer Cake

·698 字·2 分钟
B - Layer Cake **Time Limit:**6000MS **Memory Limit:**524288KB 64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 589B Description Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping and bought n rectangular cake layers. The length and the width of the i-th cake layer were a__i and b__i respectively, while the height of each cake layer was equal to one.