2017 ACM

Wesley13
• 阅读 608

Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random generators like the linear congruent generator suck. That's why he decided to invent his own random generator.

As any reasonable competitive programmer, he loves trees. His generator starts with a tree with numbers on each node. To compute a new random number, he picks a rooted subtree and multiply the values of each node on the subtree. He also needs to compute the number of divisors of the generated number (because of cryptographical applications).

In order to modify the tree (and hence create different numbers on the future), Tamref decided to perform another query: pick a node, and multiply its value by a given number.

Given a initial tree T, where T__u corresponds to the value on the node u, the operations can be summarized as follows:

  • RAND: Given a node u compute 2017 ACM and count its divisors, where T(u) is the set of nodes that belong to the subtree rooted at u.
  • SEED: Given a node u and a number x, multiply T__u by x.

Tamref is quite busy trying to prove that his method indeed gives integers uniformly distributed, in the meantime, he wants to test his method with a set of queries, and check which numbers are generated. He wants you to write a program that given the tree, and some queries, prints the generated numbers and count its divisors.

Tamref has told you that the largest prime factor of both T__u and x is at most the Tamref's favourite prime: 13. He also told you that the root of T is always node 0.

2017 ACM

The figure shows the sample test case. The numbers inside the squares are the values on each node of the tree. The subtree rooted at node 1 is colored. The RAND query for the subtree rooted at node 1 would generate 14400, which has 63 divisors.

Input

The first line is an integer n (1 ≤ n ≤ 105), the number of nodes in the tree T. Then there are n - 1 lines, each line contains two integers u and v (0 ≤ u, v < n) separated by a single space, it represents that u is a parent of v in T. The next line contains n integers, where the i - th integer corresponds to T__i (1 ≤ T__i ≤ 109). The next line contains a number Q (1 ≤ Q ≤ 105), the number of queries. The final Q lines contain a query per line, in the form "RAND u" or "SEED u x" (0 ≤ u < n, 1 ≤ x ≤ 109).

Output

For each RAND query, print one line with the generated number and its number of divisors separated by a space. As this number can be very long, the generated number and its divisors must be printed modulo 109 + 7.

Example

Input

80 10 21 32 42 53 63 77 3 10 8 12 14 40 153RAND 1SEED 1 13RAND 1

Output

14400 63187200 126题意:给一颗树共n个点,以及其结点的数值ai,有q次行为,查询询问子树(包括节点)的数值的积,与更新单个节点即乘题给数(一开始以为是整个子树都要更新)思路:明显的线段树题,但是问题是线段树里存的是什么。如果直接存积,数组开long long也存不下。那么就需要换种思路,存积的质因子的指数。即把积X=(p1^a)*(p2^b)*(p3^c)*······中的a,b,c用数组记录。又题目给出子树结点的积可以用不超过13的素数的积来表示。则定义一个b[]={2,3,5,7,11,13}。就能表示所有子树积。这样查询到以后可以直接用快速幂求得第一问,用乘法原理因子数=(a+1)*(b+1)······即得第二问。想到这里剩下的操作就是套+改线段树dfs序板子了(这里膜一下月老tql)

2017 ACM 2017 ACM

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<iostream>
  5 #include<vector>
  6 #include<queue>
  7 #include<cmath>
  8 #include<set>
  9 #define lid id<<1
 10 #define rid id<<1|1
 11 #define INF 0x3f3f3f3f
 12 #define LL long long
 13 #define debug(x) cout << "[" << x << "]" << endl
 14 using namespace std;
 15 const int maxn = 1e5+5;
 16 int b[]={2,3,5,7,11,13};
 17 int d[maxn][6]={0};
 18 void cal(int x, int *d)
 19 {
 20     for(int i = 0;i < 6 ;i++){
 21         while(x%b[i]==0)x/=b[i],d[i]++;
 22     }
 23 }
 24 const int mx = 1e5+10;
 25 const int mod = 1e9+7;
 26 int L[mx], R[mx], p[mx];
 27 struct tree{
 28     int l, r;
 29     int p[6]; //2,3,5,7,11,13
 30     int lazy[6];
 31 }tree[mx<<2];
 32 vector<int> G[mx];
 33 int cnt;
 34 LL Ans[6] = {0};
 35 
 36 LL qpow(LL x, LL n){ //x^n
 37     LL res = 1;
 38     while (n > 0){
 39         if (n & 1) res = res*x%mod;
 40         x = x*x % mod;
 41         n >>= 1;
 42     }
 43     return res;
 44 }
 45 
 46 void push_up(int id){
 47     for (int i = 0; i < 6; i++)
 48         tree[id].p[i] = tree[lid].p[i]+tree[rid].p[i];
 49 }
 50 
 51 void build(int l, int r, int id){
 52     tree[id].l = l;
 53     tree[id].r = r;
 54     for (int i = 0; i < 6; i++) tree[id].p[i] = 0;
 55     if (l == r) return;
 56     int mid = (l+r) >> 1;
 57     build(l, mid, lid);
 58     build(mid+1, r, rid);
 59 }
 60 
 61 void dfs(int u){
 62     L[u] = ++cnt;
 63     int len = G[u].size();
 64     for (int i = 0; i < len; i++){
 65         int v = G[u][i];
 66         dfs(v);
 67     }
 68     R[u] = cnt;
 69 }
 70 
 71 void upd(int c, int id, int *x){
 72     if (tree[id].l == c && tree[id].r == c){
 73         for (int i = 0; i < 6; i++)
 74             tree[id].p[i] += x[i];
 75         return;
 76     }
 77     int mid = (tree[id].l + tree[id].r)>>1;
 78     if (c <= mid) upd(c, lid, x);
 79     else upd(c, rid, x);
 80     push_up(id);
 81 }
 82 
 83 void query(int l, int r, int id){
 84     if (tree[id].l == l && tree[id].r == r){
 85         for (int i = 0; i < 6; i++)
 86             Ans[i] += tree[id].p[i];
 87         return;
 88     }
 89     int mid = (tree[id].l + tree[id].r)>>1;
 90     if (r <= mid) query(l, r, lid);
 91     else if (mid < l) query(l, r, rid);
 92     else {
 93         query(l, mid, lid);
 94         query(mid+1, r, rid);
 95     }
 96 }
 97 
 98 int main(){
 99     int n, u, v, a, q;
100     cnt = 0;
101     scanf("%d", &n);
102     for (int i = 1; i < n; i++){
103         scanf("%d%d", &u, &v);
104         G[u].push_back(v);
105         p[v] = u;
106     }
107     for (int i = 0; i < n; i++){
108         if (!p[i]) {
109             dfs(i);
110             break;
111         }
112     }
113     build(1, n, 1);
114     for (int i = 0; i < n; i++){
115         scanf("%d", &a);
116         cal(a,d[i]);
117         upd(L[i], 1, d[i]);
118     }
119     scanf("%d", &q);
120     while (q--){
121         char s[10];
122         int d2[6] = {0};
123         scanf("%s%d", s, &a);
124         if (s[0] =='R'){
125             memset(Ans, 0, sizeof Ans);
126             query(L[a], R[a], 1);
127             LL ans = 1;
128             LL num = 1;
129             for (int i = 0; i < 6; i++){
130                 num = (num*qpow(b[i], Ans[i]))%mod;
131                 ans = ans*(Ans[i]+1)%mod;
132             }
133             printf("%lld %lld\n", num, ans);
134         }
135         else {
136             int c;
137             scanf("%d", &c);
138             cal(c, d2);
139             upd(L[a], 1, d2);
140         }
141     }
142     return 0;
143 }

View Code

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Docker 部署SpringBoot项目不香吗?
  公众号改版后文章乱序推荐,希望你可以点击上方“Java进阶架构师”,点击右上角,将我们设为★“星标”!这样才不会错过每日进阶架构文章呀。  !(http://dingyue.ws.126.net/2020/0920/b00fbfc7j00qgy5xy002kd200qo00hsg00it00cj.jpg)  2
Wesley13 Wesley13
3年前
35岁是技术人的天花板吗?
35岁是技术人的天花板吗?我非常不认同“35岁现象”,人类没有那么脆弱,人类的智力不会说是35岁之后就停止发展,更不是说35岁之后就没有机会了。马云35岁还在教书,任正非35岁还在工厂上班。为什么技术人员到35岁就应该退役了呢?所以35岁根本就不是一个问题,我今年已经37岁了,我发现我才刚刚找到自己的节奏,刚刚上路。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
11个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这