A. Arpa’s hard exam and Mehrdad’s naive cheat
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There exists an island called Arpa’s land, some beautiful girls live there, as ugly ones do.

Mehrdad wants to become minister of Arpa’s land. Arpa has prepared an exam. Exam has only one question, given n, print the last digit of 1378n.

Mehrdad has become quite confused and wants you to help him. Please help, although it's a naive cheat.

Input

The single line of input contains one integer n (0  ≤  n  ≤  109).

Output

Print single integer — the last digit of 1378n.

Examples
input
1
output
8
input
2
output
4
Note

In the first example, last digit of 13781 = 1378 is 8.

In the second example, last digit of 13782 = 1378·1378 = 1898884 is 4.

题意:给你一个n,求 1387^n%10;(^为次方)

思路:循环节,特判0,或者快速幂;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
int quick(int a,int b,int mod)
{
int ans=;
while(b)
{
if(b&)ans*=a,ans%=mod;
b>>=;
a=(a*a)%mod;
}
return ans;
}
int main()
{
int n;
scanf("%d",&n);
printf("%d\n",quick(,n,));
return ;
}
B. Arpa’s obvious problem and Mehrdad’s terrible solution
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There are some beautiful girls in Arpa’s land as mentioned before.

Once Arpa came up with an obvious problem:

Given an array and a number x, count the number of pairs of indices i, j (1 ≤ i < j ≤ n) such that , where  is bitwise xoroperation (see notes for explanation).

Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.

Input

First line contains two integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 105) — the number of elements in the array and the integer x.

Second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the elements of the array.

Output

Print a single integer: the answer to the problem.

Examples
input
2 3
1 2
output
1
input
6 1
5 1 2 3 4 1
output
2
Note

In the first sample there is only one pair of i = 1 and j = 2.  so the answer is 1.

In the second sample the only two pairs are i = 3, j = 4 (since ) and i = 1, j = 5 (since ).

A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR.

题意:给你n个数,求ai^aj==x(1<=i<j<=n,^为异或符号),求i,j的总对数;

思路:标记,将式子转换一下,ai==x^aj;枚举a[i],标记a[j]得到答案;

     坑点:1:当x为0的时候,ai==aj,所以对答案的贡献是flag[ai]*(flag[ai]-1);

      2:处理过程中,答案会超过int;

      3:两个数的异或可能超过1e5;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e6+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
ll a[N],flag[N];
int main()
{
int n,x;
scanf("%d%d",&n,&x);
for(int i=;i<=n;i++)
scanf("%lld",&a[i]),flag[a[i]]++;
ll ans=;
for(int i=;i<=;i++)
{
int z=i^x;
if(z==i)
ans+=(flag[z]*(flag[z]-));
else
ans+=flag[z]*flag[i];
}
printf("%lld\n",ans/);
return ;
}
C. Arpa's loud Owf and Mehrdad's evil plan
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

As you have noticed, there are lovely girls in Arpa’s land.

People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.

Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.

The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeatedt times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called theJoon-Joon of the round. There can't be two rounds at the same time.

Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from yx would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.

Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).

Input

The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land.

The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush.

Output

If there is no t satisfying the condition, print -1. Otherwise print such smallest t.

Examples
input
4
2 3 1 4
output
3
input
4
4 4 4 4
output
-1
input
4
2 1 4

题意:找到一个最小的t,x走t步走到y,y也必须走t步走到x,x->a[x]->a[a[x]]->....->y;

思路:当a[i]为1-n且每个数都不相等,否则为-1;

   暴力找环,当环的大小x为奇数时,这个环的贡献为x;

        当环的大小x为偶数时,这个环的贡献为x/2;

   求所有环的贡献的lcm;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define esp 0.00000000001
const int N=1e3+,M=1e6+,inf=1e9;
const ll INF=1e18+;
int a[N],flag[N];
ll ans=;
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
if(flag[a[i]])return puts("-1");
flag[a[i]]=;
}
memset(flag,,sizeof(flag));
for(int i=;i<=n;i++)
{
if(flag[i])continue;
ll hh=;
int x=i;
while(!flag[x])
{
flag[x]=;
hh++;
x=a[x];
}
if(hh&)
ans=ans*hh/__gcd(hh,ans);
else
ans=ans*(hh/)/__gcd(ans,hh/);
}
printf("%lld\n",ans);
return ;
}
D. Arpa's weak amphitheater and Mehrdad's valuable Hoses
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Just to remind, girls in Arpa's land are really nice.

Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≤ i < k, and a1 = x and ak = y.

Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it.

Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w.

Input

The first line contains integers nm and w (1  ≤  n  ≤  1000, , 1 ≤ w ≤ 1000) — the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.

The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 1000) — the weights of the Hoses.

The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106) — the beauties of the Hoses.

The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≤ xi, yi ≤ nxi ≠ yi), meaning that Hoses xiand yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.

Output

Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w.

Examples
input
3 1 5
3 2 5
2 4 2
1 2
output
6
input
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3
output
7
Note

In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6.

In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.

题意:n个人去参加派对,m对关系,w为你可以带走的重量;

   每个人手中有一个礼物,一个重量w,一个美丽程度b;

   m对关系两个人是朋友;

   对于多个朋友,要么带走全部礼物,要么带走一个,要么都不带走;

思路:并查集+分组背包;

   并查集分组;

   对于分组背包,每组中再加入一个全部礼物;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define esp 0.00000000001
const int N=1e3+,M=1e6+,inf=1e9;
const ll INF=1e18+;
int fa[N];
int Find(int x)
{
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
void update(int x,int y)
{
int u=Find(x);
int v=Find(y);
if(u!=v)
{
fa[u]=v;
}
}
vector<pair<int,int> >f[N];
ll sumw[N],sumb[N];
int si;
int w[N],b[N],flag[N];
ll dp[N][N];
int main()
{
int n,val,m;
scanf("%d%d%d",&n,&m,&val);
for(int i=;i<=n;i++)
fa[i]=i;
for(int i=;i<=n;i++)
scanf("%d",&w[i]);
for(int i=;i<=n;i++)
scanf("%d",&b[i]);
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
update(u,v);
}
si=;
for(int i=;i<=n;i++)
{
int yy=Find(i);
if(!flag[yy])
{
si++;
flag[yy]=si;
f[si].push_back(make_pair(w[i],b[i]));
sumw[si]+=w[i];
sumb[si]+=b[i];
}
else
{
f[flag[yy]].push_back(make_pair(w[i],b[i]));
sumw[flag[yy]]+=w[i];
sumb[flag[yy]]+=b[i];
}
}
for(int i=;i<=n;i++)
{
if(flag[i])
{
f[flag[i]].push_back(make_pair(sumw[flag[i]],sumb[flag[i]]));
}
}
/*for(int i=1;i<=si;i++)
{
for(int j=0;j<f[i].size();j++)
cout<<f[i][j].first<<" "<<f[i][j].second<<" "<<endl;
cout<<"~~~~~~~~~~~~"<<endl;
}*/
for(int i=;i<=si;i++)
{
for(int j=;j<f[i].size();j++)
{
for(int k=val;k>=;k--)
{
dp[i][k]=max(dp[i-][k],dp[i][k]);
if(k>=f[i][j].first)
dp[i][k]=max(dp[i][k],dp[i-][k-f[i][j].first]+f[i][j].second);
}
}
}
printf("%lld\n",dp[si][val]);
return ;
}

Codeforces Round #383 (Div. 2) A,B,C,D 循环节,标记,暴力,并查集+分组背包的更多相关文章

  1. Codeforces Round #383 (Div. 2)D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(dp背包+并查集)

    题目链接 :http://codeforces.com/contest/742/problem/D 题意:给你n个女人的信息重量w和美丽度b,再给你m个关系,要求邀请的女人总重量不超过w 而且如果邀请 ...

  2. Codeforces Round #383 (Div. 2) 题解【ABCDE】

    Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...

  3. Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan

    C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...

  4. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)

    题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...

  5. Codeforces Round #383 Div 1题解

    第一次打Div 1,感觉还是挺难的..把基础题打完就日常划水了.... [A. Arpa's loud Owf and Mehrdad's evil plan](http://codeforces.c ...

  6. Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环

    题目链接:http://codeforces.com/contest/742/problem/C C. Arpa's loud Owf and Mehrdad's evil plan time lim ...

  7. dfs + 最小公倍数 Codeforces Round #383 (Div. 2)

    http://codeforces.com/contest/742/problem/C 题目大意:从x出发,从x->f[x] - > f[f[x]] -> f[f[f[x]]] -& ...

  8. 01背包dp+并查集 Codeforces Round #383 (Div. 2)

    http://codeforces.com/contest/742/problem/D 题目大意:有n个人,每个人有重量wi和魅力值bi.然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和 ...

  9. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

随机推荐

  1. 【iCore3 双核心板_FPGA】例程四:Tcl脚本实验——配置引脚

    实验指导书及代码包下载: http://pan.baidu.com/s/1pJZDz0v iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  2. Oracle的自动统计信息不收集直方图的信息

    Oracle的自动统计信息不收集直方图的信息 在oracle9i中,默认的统计信息收集是不收集直方图信息的,也就是说默认的MOTHOD_OPT模式为FOR ALL COLUMNS SIZE 1 在10 ...

  3. DevExpress GridView加入DevExpress中的右键菜单PopuMenu

    1. 添加一个Barmanager控件 2. 加入popumenu控件,点击该控件右上角的黑色三角号,编辑选项,点击编辑的选项,选择事件,编辑事件. 3. 在使用该右键菜单的控件添加MouseUp事件 ...

  4. Windows7中IIS简单安装与配置(详细图解)

    最近工作需要IIS,自己的电脑又是Windows7系统,找了下安装的方法,已经安装成功.在博客里记录一下,给需要的朋友,也是给自己留个备份,毕竟我脑子不是很好使. 一.首先是安装IIS.打开控制面板, ...

  5. Java 并发性和多线程

    一.介绍 在过去单 CPU 时代,单任务在一个时间点只能执行单一程序.之后发展到多任务阶段,计算机能在同一时间点并行执行多任务或多进程.虽然并不是真正意义上的“同一时间点”,而是多个任务或进程共享一个 ...

  6. uniq-删除重复

    uniq常用于管道中,用来删除已使用sort排序完成的重复记录. uniq有3个好用的选项: -c 可在每个输出行之前加上该行重复的次数: -d 仅显示重复的行 -u 仅显示未重复的行

  7. 安装eclipse的hadoop开发环境

    eclipse:安装路径/usr/local/eclipse hadoop-2.5.2 /usr/local/hadoop/hadoop-2.5.2 hadoop2x-eclipse-plugin-m ...

  8. Leetcode: Repeated Substring Pattern

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  9. dede 数据库类使用列表

    dedecms的数据库操作类,非常实用,在二次开发中尤其重要,这个数据库操作类说明算是奉献给大家的小礼物了. 引入common.inc.php文件 require_once (dirname(__FI ...

  10. csuoj 1505: 酷酷的单词

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1505 1505: 酷酷的单词 时间限制: 1 Sec  内存限制: 128 MB 提交: 340  ...