9.29 Codeforces Div2

9.29 Codeforces Div2

contest:

A. Distinct Digits

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have two integers ll and rr. Find an integer xx which satisfies the conditions below:

l≤x≤rl≤x≤r.
All digits of xx are different.
If there are multiple answers, print any of them.

Input
The first line contains two integers ll and rr (1≤l≤r≤1051≤l≤r≤105).

Output
If an answer exists, print any of them. Otherwise, print −1−1.

Examples
inputCopy
121 130
outputCopy
123
inputCopy
98766 100000
outputCopy
-1
Note
In the first example, 123123 is one of the possible answers. However, 121121 can’t be the answer, because there are multiple 11s on different digits.

In the second example, there is no valid answer.

#include<iostream>
using namespace std;
 
int main(){
	int l,r,i,j,k,ans;
	int a[10];
	cin>>l>>r;
	bool t;
	i=l;
	t=false;
	while(!t && i<=r){
		k=i;
		for(j=0;j<=9;j++) a[j]=0;
		while(k>0){
			a[k%10]++;
			k=k/10;
		}
		t=true;
		for (j=0;j<=9;j++) if(a[j]>=2) t=false;
		i++;
	}
	if (t) cout<<i-1;
	else cout<<-1;
	return 0;
} 

纯模拟,暴力就行了。找到各位数都不同的数字

B. Filling the Grid

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Suppose there is a h×wh×w grid consisting of empty or full cells. Let’s make some definitions:

riri is the number of consecutive full cells connected to the left side in the ii-th row (1≤i≤h1≤i≤h). In particular, ri=0ri=0 if the leftmost cell of the ii-th row is empty.
cjcj is the number of consecutive full cells connected to the top end in the jj-th column (1≤j≤w1≤j≤w). In particular, cj=0cj=0 if the topmost cell of the jj-th column is empty.
In other words, the ii-th row starts exactly with riri full cells. Similarly, the jj-th column starts exactly with cjcj full cells.

These are the rr and cc values of some 3×43×4 grid. Black cells are full and white cells are empty.
You have values of rr and cc. Initially, all cells are empty. Find the number of ways to fill grid cells to satisfy values of rr and cc. Since the answer can be very large, find the answer modulo 1000000007(109+7)1000000007(109+7). In other words, find the remainder after division of the answer by 1000000007(109+7)1000000007(109+7).

Input
The first line contains two integers hh and ww (1≤h,w≤1031≤h,w≤103) — the height and width of the grid.

The second line contains hh integers r1,r2,…,rhr1,r2,…,rh (0≤ri≤w0≤ri≤w) — the values of rr.

The third line contains ww integers c1,c2,…,cwc1,c2,…,cw (0≤cj≤h0≤cj≤h) — the values of cc.

Output
Print the answer modulo 1000000007(109+7)1000000007(109+7).

Examples
inputCopy
3 4
0 3 1
0 2 3 0
outputCopy
2
inputCopy
1 1
0
1
outputCopy
0
inputCopy
19 16
16 16 16 16 15 15 0 5 0 4 9 9 1 4 4 0 8 16 12
6 12 19 15 8 6 19 19 14 6 9 16 10 11 15 4
outputCopy
797922655
Note
In the first example, this is the other possible case.

In the second example, it’s impossible to make a grid to satisfy such rr, cc values.

In the third example, make sure to print answer modulo (109+7)(109+7).

#include<iostream>
using namespace std;
 
int main(){
	static int a[2000][2000];
	long long h,w,i,j,k,ans,m,n;
	m=1000000007;
	bool t;
	t=true;
	int r[2000],c[2000];
	cin>>h>>w;
	for(i=1;i<=h;i++)
		for(j=1;j<=w;j++) a[i][j]=0;
	for (i=1;i<=h;i++) 
	{
		cin>>r[i];
		for(j=1;j<=r[i];j++) {
			if (a[i][j]==0) a[i][j]=1;
		}
		a[i][r[i]+1]=2;
	}
	for (i=1;i<=w;i++){
		cin>>c[i];
		for(j=1;j<=c[i];j++){
			if(a[j][i]==2) t=false;
			a[j][i]=1;
		}
		if (a[c[i]+1][i]==1) t=false;
		a[c[i]+1][i]=2;
	}
	if (!t) cout<<0<<endl;
	else{
	
	ans=0;
	for(i=1;i<=h;i++)
		for(j=1;j<=w;j++)
			if(a[i][j]==0) ans++;
	long long b=1,aa=2;
	if (ans==0) cout<<1<<endl;
	else{
	
		while(ans){
			if(ans&1==1)
			b=(aa*b)%m;
			aa=(aa*aa)%m;
			ans=ans>>1;
		}
		cout<<b<<endl;
	}
	
	}
	return 0;
}

暴力,按要求模拟涂色。如果冲突了就是0,否则就统计有多少个自由格子,快速幂求最后结果。一开始傻了,还特判了零个格子是0,WA在第四个点,之后一想不对,有冲突是0,零个自由格子是2^0=1,不需特判。

总结:

掉了一分,还是手速不够快。然后WA的那一发也很可惜。细节还是要注意。

补题:

C. Primes and Multiplication

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s introduce some definitions that will be needed later.

Let prime(x)prime(x) be the set of prime divisors of xx. For example, prime(140)={2,5,7}prime(140)={2,5,7}, prime(169)={13}prime(169)={13}.

Let g(x,p)g(x,p) be the maximum possible integer pkpk where kk is an integer such that xx is divisible by pkpk. For example:

g(45,3)=9g(45,3)=9 (4545 is divisible by 32=932=9 but not divisible by 33=2733=27),
g(63,7)=7g(63,7)=7 (6363 is divisible by 71=771=7 but not divisible by 72=4972=49).
Let f(x,y)f(x,y) be the product of g(y,p)g(y,p) for all pp in prime(x)prime(x). For example:

f(30,70)=g(70,2)⋅g(70,3)⋅g(70,5)=21⋅30⋅51=10f(30,70)=g(70,2)⋅g(70,3)⋅g(70,5)=21⋅30⋅51=10,
f(525,63)=g(63,3)⋅g(63,5)⋅g(63,7)=32⋅50⋅71=63f(525,63)=g(63,3)⋅g(63,5)⋅g(63,7)=32⋅50⋅71=63.
You have integers xx and nn. Calculate f(x,1)⋅f(x,2)⋅…⋅f(x,n)mod(109+7)f(x,1)⋅f(x,2)⋅…⋅f(x,n)mod(109+7).

Input
The only line contains integers xx and nn (2≤x≤1092≤x≤109, 1≤n≤10181≤n≤1018) — the numbers used in formula.

Output
Print the answer.

Examples
inputCopy
10 2
outputCopy
2
inputCopy
20190929 1605
outputCopy
363165664
inputCopy
947 987654321987654321
outputCopy
593574252
Note
In the first example, f(10,1)=g(1,2)⋅g(1,5)=1f(10,1)=g(1,2)⋅g(1,5)=1, f(10,2)=g(2,2)⋅g(2,5)=2f(10,2)=g(2,2)⋅g(2,5)=2.

In the second example, actual value of formula is approximately 1.597⋅101711.597⋅10171. Make sure you print the answer modulo (109+7)(109+7).

In the third example, be careful about overflow issue.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
 
using namespace std;
typedef long long ll;
const long long M=1e9+7;
const long long N=1e6+10;
 
long long n,ans,x,i,j;
int  prime[N],p[N];
 
void P(){
	memset(prime,0,sizeof(prime));
	for(i=2;i<=N;i++){
		if(!prime[i]) prime[++prime[0]]=i;
		for(int j=1;j<=prime[0]&&prime[j]<=N/i;j++){
			prime[prime[j]*i]=1;
			if(i%prime[j]==0) break;
		}
	}
}
long long quick_power(long long a,long long b,long long p){
	long long x=1;
	while(b){
		if (b&1)x=(x*a)%p;
		b>>=1;
		a=(a*a)%p;
	}
	return x;
}
int main(){
	P();
	cin>>x>>n;
	p[0]=0;
	int temp=x;
	for(int i=1;i<=prime[0]&& 1ll *prime[i]*prime[i]<=x;i++){
		if(x%prime[i]==0){
			while(temp % prime[i]==0) temp/=prime[i];
			p[++p[0]]=prime[i];
		}
	}
	if(temp>1){
		p[++p[0]]=temp;
	}
	ans=1ll;
	for(int i=1;i<=p[0];i++){
		long long t=0ll,s=p[i];
		while(1){
			t=t+n/s;
			s=s*p[i];
			if(s>n/p[i]){
				t=t+n/s;
				break;
			}
		}
		ans=ans*quick_power(p[i],t,M);
		ans=ans%M;
	}
	cout<<ans<<endl;
	return 0;
}

算阶乘的素因子。多个板子的结合吧。素数筛法+快速幂,阶乘素因子。本题一个是题意理解比较费劲,第二就是数据范围,长整型要控制好。感谢同学提供的想法和思路,我一开始没想明白题意要怎么转换。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值