6.2 牛客算法週週練9

Powered by:AB_IN 局外人

A 符合條件的整數

1.區間問題(區間內符合xx條件的數)

注意題目裏區間是左閉右開,要想求區間內符合xx條件的值。則左區間--,右區間--
哎?那要是左開右閉呢? 左區間,右區間 就不用動。所以求區間滿足什麼條件時,是按左開右閉求的。
左開右開可咋辦? 左區間,右區間--
左閉右閉呢?左區間--,右區間

花了好久才琢磨透。

其實也可以單獨判斷端點處的情況,那樣比較安心一點。

2.餘數問題

要是求i%7==0的數,讓區間端點 // 7 即可。
要是i%7==1呢? 讓(端點處-1) //7 即可。

3.代碼

Py

n,m=map(int,input().split())
n=1<<n;m=1<<m
print((m-2)//7-(n-2)//7)

C++
第一次寫帶__int128 的代碼,這個數值大。
注意兩點:

  1. __int128不能直接與別的數據類型運算。(pow(n,m),m還是可以int的)
  2. 不能用cout printf輸出,得自己寫輸入輸出。
#include<bits/stdc++.h>
using namespace std;
__int128 l,r,c=1,n,m;
inline __int128 read() {__int128 s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; }
inline void write(__int128 x) { if (!x) { putchar('0'); return; } char F[200]; __int128 tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';     tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]); }
int main()
{
    n=read();m=read();
    l =c << n ; r = c << m;
    write((r-2)/7 - (l-2)/7);
    return 0;
}

也可以

#include <bits/stdc++.h>
typedef unsigned long long ull;
using namespace std;
ull n,m;
int main()
{
    cin>>n>>m;
    n=1uLL<<n,m=1uLL<<m;
    cout<<(m-2)/7-(n-2)/7<<endl;
}

1uLL 把int類型的變量轉化爲 unsigned long long,然後再賦值給unsigned long long類型的變量。(保證後面運算不會溢出ull)

還可以

會tle,但也是種方法,打表2的n次方。

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
ll n,m,ans,f[69],cnt;
void init(){
    ll a=1;
    for(int i=0;i<68;i++){
        f[i]=a;
        a*=2;
    }
}
inline ll read() {ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; }
inline void write(ll x) { if (!x) { putchar('0'); return; } char F[200]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';     tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]); }
int main(){
    init();
    n=read();m=read();
    ans=f[n];
    while(ans%7!=1) ans++;
    for(;ans<f[m];ans+=7) cnt++;
    write(cnt);
    return 0;
}

B Relic Discovery

t=int(input())
while(t>0):
    t-=1
    n=int(input());ans=0
    for i in range(n):
        x,y=map(int, input().split())
        ans+=x*y
    print(ans)

用pypy3 和 Python3(快) 耗時差距有點大。

完結。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章