題意:n盞燈,開始時都是關閉的,每次對區間內的燈的狀態進行翻轉,問最終有幾盞燈是開着的。
特別注意:內存+時間(我們第一發t了
題解:只有區間的反轉次數爲奇數次的時候纔有效,可以對l和r進行排序然後求區間和。左區間減一也行,右區間加一也行,按住一個進行變動即可(保證作差後爲區間內所有更換過狀態的燈的數目)。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int ans[2005];
int main()
{
int t;
scanf("%d",&t);
for(int cas = 1; cas <= t; ++cas)
{
int n,m;
scanf("%d%d",&n,&m);
int cc = 0;
for(int i = 0; i < m; ++i)
{
int l,r;
scanf("%d%d",&l,&r);
ans[cc++] = l-1;
ans[cc++] = r;
}
sort(ans,ans+cc);
int res = 0;
for(int i = 1; i < cc; i += 2)
res += ans[i]-ans[i-1];
printf("Case #%d: %d\n",cas,res);
}
return 0;
}
題意:給定n和b,求1~n所有的數在b進制下的數位和。
題解:暴力。
#include <iostream>
#include <algorithm>
using namespace std;
int res[11][1000005] = {0};
int mo(int b,int n)
{
int sum = 0;
while(n)
{
sum += n%b;
n /= b;
}
return sum;
}
void init()
{
for(int i = 2; i <= 10; ++i)
for(int j = 1; j <= 1000000; ++j)
{
res[i][j] = res[i][j-1] + mo(i,j);
}
}
int main()
{
init();
int t;
scanf("%d",&t);
for(int cas = 1; cas <= t; ++cas)
{
int n,b;
scanf("%d%d",&n,&b);
printf("Case #%d: %d\n",cas,res[b][n]);
}
}
歡迎指出錯誤qwq