1. 程式人生 > >codeforces 768 B Code For 1 (數學)

codeforces 768 B Code For 1 (數學)

題意:

一個集合裡一開始只有n這個數,每次操作把集合裡大於1的數分為,,,三個數,它們的位置在原位置,知道集合裡所有的數都是0和1.現在迴圈l到r區間內有多少個1,保證r-l<=1e5.

例如:

解題思路:

一開始用連結串列去模擬做,記憶體炸了.

然後開始想去計算的辦法.我們可以發現偶數位一定是1,然後奇數為取決於n這個數每次mod2的數,除開最後一次mod2得到的1之外,mod2得到的數都在奇數位上,然後我們去找這些數出現位置的規律,首先我們求出nmod2的所有數,然後我們知道n能被2除x此,然後對應到序列裡,我們容易發現第i此mod2得到的數,第一次出現在序列2的x-i-1次,並且每隔2的x-i此出現,所以我們對於l到r區間內每一個偶數位出現的數,只需要去驗證它的位置滿足那個nmod2得到的數的位置的規律就行了。

注意特判n為0的情況,賽時忘了加掛了,QAQ

程式碼:

#include <bits/stdc++.h>

using namespace std;
int a[100];
int main()
{
    long long n, l, r;
    cin>>n>>l>>r;
    if(n==0)return 0*printf("0\n");
    int i=0;
    while(n)
    {
        a[i++]=n%2;
        n>>=1;
    }
    i--;
    int len=i;
    int sum=(r-l+1)/2;
    if(((r-l+1)&1) && r&1)sum++;
    long long s=l&1?l+1:l;
    for(; s<=r; s+=2)
    {
        for(i=0; i<len; i++)
        {
            if((s-(1LL<<len-i))%(1LL<<len-i+1)==0)sum+=a[i];
        }
    }

    printf("%d\n", sum);
    return 0;
}


相關推薦

codeforces 768 B Code For 1 (數學)

題意: 一個集合裡一開始只有n這個數,每次操作把集合裡大於1的數分為,,,三個數,它們的位置在原位置,知道集合裡所有的數都是0和1.現在迴圈l到r區間內有多少個1,保證r-l<=1e5. 例如: 解題思路: 一開始用連結串列去模擬做,記憶體炸了. 然後開始想去計算

http://codeforces.com/problemset/problem/768/B B. Code For 1

scan 通過 class 題目 num continue lower scn printf 1 #include <bits/stdc++.h> 2 using namespace std; 3 long long bit[70]; 4 void

Codeforces 768B - Code For 1(分治思想)

spa pac family query strong ace sizeof tro main 768B - Code For 1 思路:類似於線段樹的區間查詢。 代碼: #include<bits/stdc++.h> using namespace st

CodeForces-768B-Code For 1+DFS類似線段樹思想

can b- include def code tar esp 操作 sca Code For 1 題意:對於一個n,可以將它分解為n/2,n%2,n/2三個數字,重復上述操作知道雖有值為1或0為止;   求L---R區間數列的和; 思路:首先畫著畫著可以發現這是一個類似線

CodeForces - 768B Code For 1 —— 遞迴分治

題意: 把一個數n拆分,奇數拆成n/2,1,n/2,偶數拆成n/2,0,n/2,1和0不可拆,問拆分後從l位置到r位置1的個數 思路: 分治遞迴拆分即可...想的太多... n最大是2^50,最多遞迴呼叫也才是高為50的二叉樹 #include <iostream>

Code For 1 CodeForces - 768B (簡單分治)

題目連結   類似於線段樹, 但分治時要處理出區間長度   #include <iostream> using namespace std; typedef long long ll; ll ql, qr, x; ll len(ll x) { retur

CodeForces768B:Code For 1 (分治)

ons 對稱 first 一個 AD enc cert ace either Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On

Codeforces768B-Code For 1-類似線段樹-枚舉+單點更新or區間更新

pre \n turn .net main std blog query amp 目錄 Catalog Solution: (有任何問題歡迎留言或私聊 && 歡迎交流討論哦 Catalog Problem:Portal傳送門 ?原題目描述在最下面。 ?

Code For 1(線段樹思想)

Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that h

Codeforces 847 B. Preparing for Merge Sort (二分)

Description Ivan has an array consisting of n different integers. He decided to reorder all eleme

Educational Codeforces Round 47 (Rated for Div. 2) A模擬 B思維 C數學 D數學,暴力

A Code: #include <bits/stdc++.h> using namespace std; const int AX = 1e3 + 66; int a[AX]; int c

Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)B. T-shirt buying

character break empty not integer contain strong accept space 傳送門 Description A new pack of n t-shirts came to a shop. Each of the t-

[CodeForces - 614B] B - Gena's Code

std specified note doesn fin imp order codeforce sed A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree

Educational Codeforces Round 37 (Rated for Div. 2)A,B,C,F

fine 暴力模擬 需要 lose sort codeforce 結構 分享圖片 線段樹 A Water The Garden 數據不大,暴力模擬下直至把每個花床都遍歷過的過程即可 1 #include <bits/stdc++.h> 2 us

Educational Codeforces Round 48 (Rated for Div. 2) B. Segment Occurrences(數組前綴和防TLE)

pre segment col -m lan 需要 contest tdi ati 題目鏈接:http://codeforces.com/contest/1016/problem/B 給兩個字符串s,t,之後給出s的一個區間,問這個子串中存在多少個子串與t相同 如果一個一個

Educational Codeforces Round 48 (Rated for Div. 2) B Segment Occurrences

mes include cin c++ end substr 是的 pre nal 翻譯 給你一個字符串\(s\)和另一個字符串\(t\),然後給你\(q\)個區間,問\(s\)在這些區間裏的子串有多少個與\(t\)相同。 思路 一道要細心的模擬題,使用\(STL stri

Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) -B C(GCD,最長連續交替序列)

all clas 拼接 true 序列 -s light scan inf B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 mega

A、B、C、D、Educational Codeforces Round 42 (Rated for Div. 2)

Educational Codeforces Round 42 (Rated for Div. 2)  http://codeforces.com/contest/962 A:Equator 這裡需要注意一個問題,就是直接 / 2 的時候、有奇數偶數的情況、如果是奇數的話、這

B. Lecture Sleep( Educational Codeforces Round 41 (Rated for Div. 2))

題目連結 http://codeforces.com/contest/961/problem/B 我的思路是、t陣列為1的就是一定能獲得的定理數、我在輸入的時候單獨的把可以直接聽得到的定理數統計出來、 對於需要刺激才能聽得到的、我將t陣列用一個類收集起來、按時間排序、(找最長子區間)

Educational Codeforces Round 54 (Rated for Div. 2) B. Divisor Subtraction

觀察易得 1.質數無1和自身外的因子 且只有本身既質又因 按題意直接一步減自身至零 2.若N是偶數則一直減2直到0 所有質數都是奇數 奇數減奇數易得偶數 再回到條件2 一步到位 所以操作次數不會太多 線篩打表 結合1 2 暴力模擬即可   /* Z