寒假集训-今天也要加油啊
Codeforces Round #589 (Div. 2)
A - Distinct Digits
因为数据很小所以直接将各位上不重复的数字标记,然后每次询问直接遍历[l,r]查找是否存在满足条件的值。
Code
#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int inf=0x3f3f3f3f;
typedef long long ll;
const int N=2e5+7;
const ll mod=1e9+7;
int main(){
IO;
for (int i = 1; i <= 1e5; ++i)
{
int k=i,cnt=0;
set<int>s;
while(k){
s.insert(k%10);
k/=10;
cnt++;
}
if(cnt==int(s.size()))mp[i]++;
}
int t=1;
//cin>>t;
while(t--){
int l,r;
cin>>l>>r;
int flag=-1;
for (int i = l; i <= r; ++i)
{
if(mp[i]){
flag=i;
break;
}
}cout<<flag<<endl;
}
return 0;
}
B - Filling the Grid
阅读理解杀我
题意是给出两个数组r和c分别表示行和列中从头开始连续的方格数,求出满足条件的填充方案。
首先可以得出第i行的1 - r[i]列必为黑色,而第r[i]+1个方格必为白色,同理第j列的1 - c[j]行必为黑色,第c[j]+1个方格必为白色,所以在不冲突的情况下答案由剩余的格子数决定。
考虑冲突的情况:第i行第j列的方格,如果r[i]>=j表示当前格子为黑色,当(c[j]+1=i)时表示当前格子为白色故冲突,答案为0;同理可得(c[j]>=i&&r[i]+1=j)也是冲突的情况。
答案直接爆搜即可
Code
#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int inf=0x3f3f3f3f;
typedef long long ll;
const int N=1e3+7;
const ll mod=1e9+7;
int r[N],c[N];
int main(){
IO;
int t=1;
//cin>>t;
while(t--){
int h,w;
cin>>h>>w;
for (int i = 1; i <= h; ++i)
{
cin>>r[i];
}
for (int i = 1; i <= w; ++i)
{
cin>>c[i];
}ll ans=1;
for (int i = 1; i <= h; ++i)
{
for (int j = 1; j <= w; ++j)
{
if((r[i]>=j&&c[j]+1==i)||(c[j]>=i&&r[i]+1==j))ans=0;
if(i>c[j]+1&&j>r[i]+1)ans=ans*2%mod;
}
}cout<<ans<<endl;
}
return 0;
}
C - Primes and Multiplication
很容易想到是筛出x的所有质因数后遍历n即可,但是因为n的范围太大,直接遍历肯定会T,所以就考虑每个质因子的贡献。
考虑第i的质因子p时,用n除以p得到以p为因子的小于n的数的个数,因为还可能存在以p2、p3、…… 为因子的数,所以依次除以p并且每次将数的个数相加(m)得到p的总贡献pm
所以直接遍历x的所有质因子求总贡献即可
Code
#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int inf=0x3f3f3f3f;
typedef long long ll;
const int N=2e5+7;
const ll mod=1e9+7;
vector<int>prime;
map<int,int>check;
ll ksm(ll x,ll y){
ll p=1;
while(y){
if(y&1)p=p*x%mod;
x=x*x%mod;
y>>=1;
}return p%mod;
}
int main(){
IO;
check[0]=check[1]=1;
for (int i = 2; i <= 1e5; ++i)
{
if(!check[i])prime.push_back(i);
for (auto j : prime)
{
if(j*i>1e5)break;
check[i*j]=1;
if(i%j==0)break;
}
}
int t=1;
//cin>>t;
while(t--){
ll n;
int x;
cin>>x>>n;
map<int,int>mp;
vector<ll>v;
for (auto i : prime)
{
if(i>x)break;
if(x%i==0)v.push_back((ll)i);
while(x%i==0){
x/=i;
mp[i]++;
}
}
if(x>1)v.push_back(x);
ll ans=1;
for (auto i : v)
{
ll k=n;
ll cnt=0;
while(k){
k/=i;
cnt+=k;
}
ans=ans*ksm(i,cnt)%mod;
}
cout<<ans<<endl;
}
return 0;
}
继续努力!!!
Code will change the world !